cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Authorization for apoc.load.jsonParams

I got the authorization working but have a question about how best to create a graph from a specific JSON. Here's a sample of the JSON:

 "items": {
   "8dec2e1cf9": {
      "itemType": "PROBLEM",
      "name": "Manual entry",
    },
    "deec2e4682": {
      "itemType": "SYSTEM",
      "name": "Excel",
     }
   "lee25020tv921":{
      "itemType:" "ACTIVITY"
      "name": "Submit final report"
      "systems": [
        {
          "val": "deec2e4682"
        }
      ]}

Essentially I'd like to create nodes for each item, with ACTIVTY, PROBLEM, and SYSTEM nodes, each with properties that include name and id. I'd also like to create a relationship between ACTIVITIES that have certain problem or systems, but am not sure how to best use merge/create commands to achieve this.

5 REPLIES 5

Your format is a bit tricky, so doing it all in one Cypher statement is not that pretty:

CALL apoc.load.json...() yield value as items
UNWIND keys(items) as key
WITH key, items[key] as item
FOREACH (_ IN case item.itemType when 'PROBLEM' | MERGE (p:Problem {id:key}) SET p.name = item.name
FOREACH (_ IN case item.itemType when 'SYSTEM' | MERGE (s:System {id:key}) SET s.name = item.name
FOREACH (_ IN case item.itemType when 'ACTVITY' | 
  MERGE (a:Activity {id:key}) SET a.name = item.name
  FOREACH (sys IN item.systems | MERGE (s:System {id:sys.val}) MERGE (s)-[:HAS_ACTIVTY]->(a) )
)
...

Thanks Michael - when I tried that I got the following error from neo4j:
"Neo.ClientError.Statement.SyntaxError: Invalid input '|': expected whitespace, comment, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', '~', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR or THEN (line 4, column 49 (offset: 325)) "FOREACH (_ IN case item.itemType when 'PROBLEM' | MERGE (p:Problem {id:key}) SET p.name = item.name"

Yes I left that part for you to fill in with your details.

I've tried the filling in the code you suggested as so:

CALL apoc.load.json … () yield value as items
UNWIND keys(items) as key
WITH key, items[key] as item
FOREACH (p IN case item.itemType when 'PROBLEM' | MERGE (p:Problem {id:key}) SET p.name = item.name)
FOREACH (s IN case item.itemType when 'SYSTEM' | MERGE (s:System {id:key}) SET s.name = item.name)
FOREACH (a IN case item.itemType when 'ACTVITY' |
MERGE (a:Activity {id:key}) SET a.name = item.name)
FOREACH (sys IN item.systems | MERGE (s:System {id:sys.val}) MERGE (s)-[:HAS_ACTIVTY]->(a) )

And I get the same error (Neo.ClientError.Statement.SyntaxError: Invalid input '|': expected whitespace...). Is there something about the syntax for FOREACH that I'm not grasping?

Oh sorry that was only a fragment, those case statements must be:

case item.itemType when 'SYSTEM' then [true] else [] end

i.e. non-empty list vs. empty list.

FOREACH (s IN case item.itemType when 'SYSTEM' then [true] else [] end | 
  MERGE (s:System {id:key}) SET s.name = item.name)