cancel
Showing results for 
Search instead for 
Did you mean: 

Creating nodes from json without apoc using python

ranjanr331
Node Link

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.

9 REPLIES 9

Cobra
Ninja
Ninja

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

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.

Cobra
Ninja
Ninja

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

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.

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.

Cobra
Ninja
Ninja

Did you try the second query I put with UNWIND?

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

vmnaidu12
Node

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.

samsitta
Node Link

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)
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.