How to extract dynamic property names from a json file in Neo4J using Cypher Query

163 Views Asked by At

The tags property names are dynamic. e.g. linux and cypher are dynamic in the json. I am trying to extract the dynamic tags and their values and associate them as properties to the Person node. Here is what I have so far:

CALL apoc.load.json("file:///example.json")
YIELD value
MERGE (p:Person {name: value.name})
ON CREATE SET 
p.job = value.job,
p.department = value.department

RETURN p as Person;

example.json:

{
    "name": "Harry",
    "job": "Developer",
    "tags": {
        "linux": "awesome",
        "cypher": "Working on it"
    },

    "department": "IT"

}
1

There are 1 best solutions below

1
On BEST ANSWER

You can assign all properties from the "tags" key with p += value.tags syntax, ie:

CALL apoc.load.json("file:///example.json")
YIELD value
MERGE (p:Person {name: value.name})
ON CREATE SET 
    p.job = value.job,
    p.department = value.department,
    p += value.tags
RETURN p as Person

It creates the following node (with your data example):

{
  "identity": 29,
  "labels": [
    "Person"
  ],
  "properties": {
    "cypher": "Working on it",
    "linux": "awesome",
    "name": "Harry",
    "job": "Developer",
    "department": "IT"
  }
}

See doc here: https://neo4j.com/docs/cypher-manual/current/clauses/set/#set-setting-properties-using-map