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.

Filter a list with multiple conditions using WITH in Cypher,Neo4j

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

1 ACCEPTED SOLUTION

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

View solution in original post

3 REPLIES 3

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

Thanks for reply, But it still give me errors.

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