cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

UNWIND error when I try to import a JSON

gorkasalauniver
Node Clone

Hey, I am trying to import a JSON, so I have to use UNWIND to get the data, but Iget no result after making an specific UNWIND.

This is the JSON:

{
    "objects": [
        {
            "id": "R1",
            "part1": {
                "text": "Globally"
            },
            "part2": {
                "text": "if {R}, then {S} in {T} time",
                "components": [
                    {
                        "actioner": {
                            "text": "A || B",
                            "type": "R",
                            "variables": [
                                {
                                    "variable": {
                                        "name": "A",
                                        "type": "inputs"
                                    }
                                },
                                {
                                    "variable": {
                                        "name": "B",
                                        "type": "constants"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "actioned": {
                            "text": "C==B",
                            "type": "S",
                            "variables": [
                                {
                                    "variable": {
                                        "name": "C",
                                        "type": "constants"
                                    },
                                    "variable": {
                                        "name": "B",
                                        "type": "constants"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "time": {
                            "text": "CONST_TIME",
                            "type": "T",
                            "variables": [
                                {
                                    "variable": {
                                        "name": "CONST_TIME",
                                        "type": "constants"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
	]
}

And this is the Cypher command:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part1 AS part1
UNWIND objects.part2 AS part2
UNWIND part2.components as components
UNWIND components.actioner AS actioner

Then if I do a return of anything I get the expected results:

return actioner

But if I add the next Cypher line it doesn´t work, I get a empty result:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part1 AS part1
UNWIND objects.part2 AS part2
UNWIND part2.components as components
UNWIND components.actioner AS actioner

> UNWIND components.actioned AS actioned

And if I ask to return actioner or actioned or anything I don't get any result.

This is what I get:
2X_9_974403586cf515d5174352783d50bfe8080a805d.png

Could anybody help me?

Thank you!

4 REPLIES 4

Keep in mind that you only need to UNWIND collections, you do not need to UNWIND maps/objects.

Therefore you do not need to do any of the following:

UNWIND objects.part1 AS part1
UNWIND objects.part2 AS part2
UNWIND components.actioner AS actioner
UNWIND components.actioned AS actioned

Instead, just use dot notation for referencing:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part2.components as component
// you can now reference actioner with `component.actioner`
// you can now reference actioned with `component.actioned`
...

If you try to UNWIND something that is not a list, it will interpret it as a single-element list for convenience, but this is not needed and should be avoided.

The reason why you were getting no results is because UNWIND creates a row per entry in a list. If the list is empty, then the row will be wiped out (0 x your input row = 0 rows).

Hi Andrew, I'm so grateful with you, you've made it.

But now I have another problem, when I reference something inside actioner or actioned I got 3 results, adn 2 of them are null.

Let's see what I'm saying.

This is the Cypher command:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part2.components as component

return component.actioner.text

And this is the result:

What's the problem in the cypher command?

Thanky you Andrew!

Nothing wrong with the Cypher command.

In the JSON snippet you provided, part2.components has 3 objects/maps in the list, but they aren't of the same type so you don't have any way to address these universally.

The first of them has an actioner property, but the other two don't.

The second of them has an actioned property, but the other two don't.

The third has a time property but the other two don't.

So the real problem here is you have a list of 3 objects but none of them have the key of their single nested object in common.

If you have any ability to reformat the data, I'd recommend pulling those up, such that components isn't a list of 3 mismatched objects, but is a map with keys for actioner, actioned, and time.

Okay, I understand what you say.

But I can't reformat the data, so what I've been thinking is to delete null values in a WHERE sentence:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part2.components as component
WITH *, component.actioner AS actioner WHERE component.actioner IS NOT NULL
return actioner.text

But I can do it once, if I do the same with actioned I get no results:

CALL apoc.load.json("example.json") YIELD value AS json
UNWIND json.objects AS objects
UNWIND objects.part2.components as component
WITH *, component.actioner AS actioner WHERE component.actioner IS NOT NULL
WITH *, component.actioner AS actioner WHERE component.actioner IS NOT NULL
return actioner.text
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online