UNWIND multiple unrelated arrays loaded from JSON file

I'm trying to UNWIND multiple array properties with one call to apoc.load.json (). The version I have is not fully working: some relationships are not loaded. I guess this is because of feeding the output through the WITH command. I can load everything if I run unwinding separately for each array based property, but I'm curious how this can be done all together.

Any ideas and pointers are appreciated =)

//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity,

WITH c, class.items AS items, class.companions AS companions, class.locations AS locations
UNWIND items AS item
UNWIND companions AS companion
UNWIND locations AS location

MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)

      

An example of a JSON file entry:

 {
    "name": "KNIGHT",
    "strength": [75,100],
    "intelligence": [40,80],
    "dexterity": [40,85],
    "items": [
        "SWORD",
        "SHIELD"
    ],
    "companions":[
        "KNIGHT",
        "SERVANT",
        "STEED"
    ],
    "locations": [
        "CASTLE",
        "VILLAGE",
        "CITY"
    ]
}

      

+3


source to share


1 answer


The actual problem here is simply unnecessary ,

between the last line of your SET clause and the WITH clause. Get rid of it and you get rid of the syntax error.

However, I highly recommend grouping each UNWIND with clauses that act on decoupled values, then resetting the power back to one before executing the next UNWIND and processing. Something like that:



//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity

WITH c, class

UNWIND class.items AS item
MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

WITH distinct c, class
UNWIND class.companions AS companion
MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

WITH distinct c, class
UNWIND class.locations AS location
MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)

      

+2


source







All Articles