Rihana
Rihana

Reputation: 53

How to have an array of objects of Json keys while using apoc.load.jason() in Neo4j?

I have the following Json, I want to create nodes for each keys which are random hash ids, I also need these hash keys for UNWIND their nested objects as nodes. how can I have an array of objects (not array of string) containing these hash keys. E.X. array = [bacf06771e0f4fc5a8e68c30fc77c9c4,948eeebd73564adab7dee5c6f177b3b9]. I need to UNWIND array[0].arg1 AS arg1

{
    "bacf06771e0f4fc5a8e68c30fc77c9c4":{
        "arg1":"the Treasury",
        "arg2":"details of the November refunding",
        "relation":"will announce",
        "id":"bacf06771e0f4fc5a8e68c30fc77c9c4",
        "linkedContexts":[
            {
                "targetID":"948eeebd73564adab7dee5c6f177b3b9",
                "classification":"CONTRAST"
            }
        ]
    },
    "948eeebd73564adab7dee5c6f177b3b9":{
        "arg1":"the funding",
        "arg2":"",
        "relation":"will be delayed",
        "id":"948eeebd73564adab7dee5c6f177b3b9",
        "linkedContexts":[
            {
                "targetID":"006a71e51295440fab7a8e8c697d2ba6",
                "classification":"CONDITION"
            }
        ]
    }
}

I tried:

Call apoc.load.json("files:/example.json") Yield value 
unwind value.sentences as v
foreach (k in keys(v)|merge (arg1:Arg1 {subject:k.arg1}) 
    merge (arg2:Arg2 {object:k.arg2}) 
    merge (arg1)-[:Related_to]->(arg2))

and got the error:

`Neo.ClientError.Statement.SyntaxError: Type mismatch: expected Any, 
Map,Node, Relationship, Point, Duration, Date, Time, LocalTime, 
LocalDateTime or DateTime but was String
(line 8, column 27 (offset:371)):"merge (arg1:Arg1 {subject:k.arg1})"

Upvotes: 0

Views: 731

Answers (1)

cybersam
cybersam

Reputation: 67019

value has no sentences key.

If your data file is exactly as shown, the following query should work.

CALL apoc.load.json("files:///example.json") YIELD value 
FOREACH (v in [k IN KEYS(value) | value[k]] |
  MERGE (arg1:Arg1 {subject:v.arg1}) 
  MERGE (arg2:Arg2 {object:v.arg2}) 
  MERGE (arg1)-[:Related_to]->(arg2))

This list comprehension generates a list of the submaps of the value map:

[k IN KEYS(value) | value[k]]

Alternatively, you could use UNWIND instead of FOREACH as follows:

CALL apoc.load.json("files:///example.json") YIELD value
UNWIND [k IN KEYS(value) | value[k]] AS v
MERGE (arg1:Arg1 {subject:v.arg1}) 
MERGE (arg2:Arg2 {object:v.arg2}) 
MERGE (arg1)-[:Related_to]->(arg2)

Upvotes: 2

Related Questions