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

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 answer

  • answered 2022-05-07 08:59 stellasia

    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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum