Head's Up! Site migration is underway. Expect disruption to service on Thursday, Feb. 9!
β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