Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-14-2021 11:06 PM
I'm new to Neo4j. I'm kind of got stuck for loading a JSON file into Neo4j database. I have a list of tags for many transactions. Each transaction tag can have one or two tags: third-party and category.
Here is an example JSON.
{
"tags": [
[
{
"thirdParty": "Supermarkets"
},
{
"category": "Groceries"
}
],
[
{
"category": "Rent"
}
],
[
{
"thirdParty": "Education"
},
{
"category": "Education"
}
]
.....
]
}
I only want to find objects that have both category and thirdParty(some objects only have one tag).
And here is my code, the list is just a list of categories I want.
CALL apoc.load.json("file:///full.json")
YIELD value
with ['Gambling','Subscription TV','Telecommunications'] as list
UNWIND value.tags as tags
WITH [tag in tags where tag.category IN list AND tag.thirdParty IS NOT NULL] AS temp
RETURN temp
The weird thing is this always return me a list null. But with only one condition then it will work, like only find objects in the list or only thirdParty is not null. But combine the 2 conditions together, it will always return a list of null.
Does anyone know how to fix this?
Thanks
Solved! Go to Solution.
09-07-2021 02:01 AM
Found solution. This will work
WITH tagsList, ['Groceries','Subscription TV','Telecommunications'] as list
UNWIND tagsList as tags
WITH list,
apoc.map.fromPairs(
REDUCE(arr=[],tag IN tags |
arr+[[keys(tag)[0],tag[keys(tag)[0]]]]
)
) AS tagMap
WHERE tagMap.category IN list AND tagMap.thirdParty IS NOT NULL
RETURN tagMap
08-15-2021 12:49 AM
CALL apoc.load.json("file:///full.json") YIELD value
UNWIND value.tags AS tag
WITH tag WHERE exists(tag.thirdParty) AND exists(tag.category)
RETURN tag
Quick Cypher, not tested but it gives you a better the logic
09-07-2021 02:02 AM
Thanks for reply, But it still give me errors.
09-07-2021 02:01 AM
Found solution. This will work
WITH tagsList, ['Groceries','Subscription TV','Telecommunications'] as list
UNWIND tagsList as tags
WITH list,
apoc.map.fromPairs(
REDUCE(arr=[],tag IN tags |
arr+[[keys(tag)[0],tag[keys(tag)[0]]]]
)
) AS tagMap
WHERE tagMap.category IN list AND tagMap.thirdParty IS NOT NULL
RETURN tagMap
All the sessions of the conference are now available online