Creating nodes from json without apoc using python

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.

Hello @ranjanr331 and welcome to the Neo4j community :slight_smile:

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.

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 :slight_smile:

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.

Did you try the second query I put with UNWIND?

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.

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

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.

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)