Reputation: 1
I want to create a Neo4j graph from data read from a json file, but I am not able to loop over the data I get as a result.
I tried to proceed as in this example: https://neo4j.com/blog/cypher-load-json-from-url/
However, the structure of my apoc.load.json result does not seem to be analogous. I am trying to figure out what I can use instead of:
UNWIND data.items as q
from the example, as for me, this isn't working.
To understand the structure of my output, I performed this from the Neo4j Browser:
CALL apoc.load.json('mytest.json') YIELD value
RETURN keys(value)
with the result:
["favorites", "sentiment", "content-length", "keywords", "published-at", "reach", "title", "facts", "posts", "normalized-flesch-score", "ext-url", "shares", "last-modified", "score", "xscore", "companies", "quote", "provider", "content-type", "created-at", "categories", "id", "lang", "value", "keyphrases", "annotation", "duplicates", "karma", "topics", "subjects", "al", "possibly-sensitive", "inserted-at", "tags", "replies", "entities", "meta", "domain", "flesch-score", "locations", "parent-id", "relations", "adult", "non-media-entities", "user", "tr", "status"]
["favorites", "sentiment", "content-length", "keywords", "published-at", "reach", "title", "facts", "posts", "normalized-flesch-score", "ext-url", "shares", "last-modified", "score", "xscore", "companies", "quote", "provider", "content-type", "created-at", "categories", "id", "lang", "value", "keyphrases", "annotation", "duplicates", "karma", "topics", "subjects", "al", "possibly-sensitive", "inserted-at", "tags", "replies", "entities", "meta", "domain", "flesch-score", "locations", "parent-id", "relations", "adult", "non-media-entities", "user", "tr", "status"]
...
However, when I try to loop over the objects in my output and return one key like this:
CALL apoc.load.json('mytest.json') YIELD value
UNWIND value AS v
RETURN v.favorites
I get an error:
Neo.ClientError.Statement.SyntaxError: Type mismatch: expected List<T> but was Map (line 2, column 8 (offset: 62))
"UNWIND value AS v"
^
On the other hand if I loop over for instance value.title rather than value, I get sensible values.
I then looked at examples of looping over a map with cypher, but the maps showed there all had a different structure and the solutions were not applicable (at least to my beginners eye) to my problem.
It would be great if someone could give me some inputs regarding how to loop over the object I have. Many thanks in advance!
Upvotes: 0
Views: 1038
Reputation: 67019
Every value
returned by apoc.load.json
is a map, not a list.
To get the value of any property (say, foo
) from a map, simply use value.foo
(or value['foo']
). For example:
CALL apoc.load.json('mytest.json') YIELD value
RETURN value.foo
Upvotes: 0
Reputation: 4052
You don't need to use unwind
here.
You can simply return favorites as:
CALL apoc.load.json('mytest.json') YIELD value
RETURN value.favorites
Upvotes: 1