Unwind creates 2 rows instead of one

apoc
cypher
unwind
import

(Papangelisang) #1

Hello,

I am new in Neo4j. I am trying to import a json(schema) file in order to see the data in the graph.
The json file looks like this:

{
"data": {
    "id": "3124test2134",
    "information": [
      {
        "name": "info name",
        "tags": ["tag1", "tag2"]
      }
    ]
}
}

When I write this:

CALL apoc.load.json("file:/data.json") YIELD value 
UNWIND (CASE value.data.information WHEN [] THEN [null] ELSE value.data.information END) AS info 
UNWIND (CASE value.data.information.tags WHEN [] THEN [null] ELSE value.data.information.tags END) AS tags
RETURN tags

I have this result:

│"info"                                     │"tags"        │
│{"name":"info name","tags":["tag1","tag2"]}│"tag1"        │
│{"name":"info name","tags":["tag1","tag2"]}│"tag2"        │

But I want something like this:

│"info"                                     │"tags"        │
│                                           │              │
│{"name":"info name","tags":["tag1","tag2"]}│"tag1"        │
│                                           │"tag2"        │

So I can have 1 row for info and 2 rows for tags. Is there any way to do that?

I have a problem later in the code when I want to create nodes. it creates 2 nodes instead of 1.

Thank you very much in advance!

/Angelos


(Mark Needham) #2

I'm not sure how to get the data in the format you want, but I'm curious about this:

I have a problem later in the code when I want to create nodes. it creates 2 nodes instead of 1.

Could you solve this problem by creating nodes using the MERGE clause or does that not help in this case?


(Andrew Bowman) #3

I'm not quite sure how you got results here.

While your first UNWIND is fine, there's a problem with this:

UNWIND (CASE value.data.information.tags ...

value.data.information is a list, so you can't access tags from it. This should throw an error.

If your file was instead this:

{
"data": {
    "id": "3124test2134",
    "information": {
        "name": "info name",
        "tags": ["tag1", "tag2"]
      }
}
}

Then you would get the results you provided (though you would have to RETURN info, tags)

To get the results you want, forgo the second unwind so the tags remain in a list per row:

CALL apoc.load.json('file:import/data.json') YIELD value 
UNWIND (CASE value.data.information WHEN [] THEN [null] ELSE value.data.information END) AS info 
RETURN info, info.tags

(Papangelisang) #4

It doesn't throw any error. It just shows the above result. But you are right, it works fine now with your tweak!
Thank you!


(Papangelisang) #5

Yes to some extend but I need to use CREATE because I need use the same json schema with different json data files every time.
Thank you very much for the response!