Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
09-01-2020 11:25 PM
Hi,
I have a python script which generates list of json like following:
[{'Identifier': 'XYZ', 'Description': '', 'Type': 'ABC', 'SubType': '', 'Date': '', 'Mnemonic': '', 'Notes': ''}]
I want to write a cypher query to create a node using this json data as the node properties and 'Type' field as the node label.
How can i proceed for this without using any apoc function calls.
Thanks.
09-02-2020 01:53 AM
Hello @ranjanr331 and welcome to the Neo4j community
Yes, it's possible:
For one node:
MERGE (n) SET n += {'Identifier': 'XYZ', 'Description': '', 'Type': 'ABC', 'SubType': '', 'Date': '', 'Mnemonic': '', 'Notes': ''}
If you have a list of dictionnaries:
UNWIND batch AS row
MERGE (n) SET n += row
Regards,
Cobra
09-02-2020 03:04 AM
Hi @Cobra.
Thanks you for the response but it didn't solve my problem. The problem is that the dictionary keys are inside single quotes however, if we remove the quotes from the keys of dictionary like: instead of 'Identifier', 'Description'...,if we use Identifier:'XYZ', Description: '' the query is working fine.
09-02-2020 03:07 AM
You are using Python to load your data right? If yes can you show me your function you are using to load your data?
I wrote a little example here
09-02-2020 03:25 AM
Hi @Cobra,
Actually, i'm not loading data. The data is with me in the form of list of dictionaries. I need to read that list of dictionaries and write a cypher query to CREATE nodes using these dictionaries. Like for a list containing only one dictionary: [{'Identifier': 'XYZ', 'Description': '', 'Type': 'ABC', 'SubType': '', 'Date': '', 'Mnemonic': '', 'Notes': ''}], one node should be created with all the dictionary data as node property. I hope it is clear.
09-02-2020 03:42 AM
Here's a python script that can ingest data into neo4j from CSV or json etc.
You can take the relevant code snippet from here.
If you are writing to a json file then you can use the yaml configuration for your file name and cypher queries.
09-02-2020 03:27 AM
Did you try the second query I put with UNWIND?
09-02-2020 03:54 AM
I tried,
WITH {Identifier: 'XYZ', Description: '', Type: 'ABC'} as value
UNWIND value as node
CREATE(m) SET m=node.
This is working fine but without quotes. Whereas, below one doesn't work:
WITH {'Identifier': 'XYZ', 'Description': '', 'Type': 'ABC'} as value
UNWIND value as node
CREATE(m) SET m=node
09-04-2020 03:16 AM
You can try using py2neo if you have so many labels and properties like:
store json list in data
from py2neo import Garph,Node
labels=["Sample"]
properties = data[0]
print(Node(*labels,**properties))
a=Node(*labels,**properties)
tx=graph.begin()
graph.create(a)
tx.commit()
You can write for loop for no.of different Nodes creation.
10-27-2020 11:16 AM
I would use the official Neo4j Driver and something like:
with driver.session() as session:
tx = session.begin_transaction()
for row in list:
parameters = {}
parameters['Identifier'] = row.get('Identifier')
parameters['Description'] = row.get('Description')
query = ''' MERGE (n:Type)
SET n.Identifier = $Identifier
n.Description = $Description'''
tx.run(query, parameters=parameters)
All the sessions of the conference are now available online