Help with Apoc Json Load

Please could someone help me. I figured out a lot but.. I am stuck..
I am trying to import JSON into my graph using the apoc.json.load mechanism.
i made a simple JSON packet which shows the structure and I am sending a small code segment for you to look at and run. In the code segment I placed my question, hopefully you can help me.
The JSON Packet:
{
"familyName":"happyFolks",
"dad": "Pete",
"mom":"Sue",
"children":[
{
"name":"Paul",
"hobbies":
},
{
"name":"Pete",
"hobbies": [
{
"sport":{"type":"baseball", "wins":10, "loses":3},
"colors":[ 32, 32, 255 ]
},
{
"sport":{"type":"football", "wins":5, "loses":3},
"colors":[ 20, 32, 124 ]
}
]
}
]
}
My Apoc Code:
CALL apoc.load.json("family.json") YIELD value
merge (fam:Family{Name:value.familyName })
merge (dad:Dad{Name:value.dad})
merge (fam)-[:goesTo]->(dad)
merge (mom:Mom{Name:value.mom})
merge (fam)-[:goesTo]->(mom)
merge (childrenRoot: Children{Name:"Children"})
merge (fam)-[:goesTo]->(childrenRoot)
With value, fam, childrenRoot
UNWIND value.children AS children
merge (child:Child{Name: children.name})
merge (childrenRoot)-[:goesTo]->(child)

// how do I handle hobbies?
// create a sport node off of each child and then I would like a baseball node and a football node off of the child’s sport node with their associated elements

With value
Match (nodes) return count(nodes)
The Graph I Generate:

How do I add in the Hobby elements? everything I try fails. what is the trick?

thanks guys! thanks man for helping me out on this..

You can to our your query with two approaches.

  1. ‘UNWIND children.hobbies as hobby’ and process each hobby
  2. Use a ‘call subquery’ to process each child’s hobbies.
Call {
with children
Unwind children.hobbies as hobby
//process each hobby
}

Which to use depends on it there is anything remaining after processing the hobbies. If not, they are effectively the same. If there is, the subquery may be better because it isolates the processing of the hobbies, allowing you to continue after the subquery more easily.

thanks Gary.. I think I did this but.. let me try again.. I guess I messed up and call subquery? Maybe I dont have that fully figured out.. sigh.. or could use some more sleep.. :)
As Always, thanks man for helping me.. Please have a good weekend! thanks man

1 Like

sorry.. any chance or you.. sorry.. posting an example.. :slight_smile:

Can you provided your existing query, test data, and what is not working as you want?

@rlukas

also a bit unrelated but be sure to have indexes to support those MERGE statements that create/update nodes. For example

merge (fam:Family{Name:value.familyName })

you should have an index on :Family(Name);

Without said indexes performance could be less than desired

correct!.. I am doing that. I am just posting a really simplified example here, just to make things easier to understand.. Thanks Dana