Return a list of strings after importing a JSON

Hello there,
I'm trying to get a list of strings without null values, but I can't get the result that I want.
I've tried it in two different ways:

WITH 'recursivo.json' AS json_file
CALL apoc.load.json(json_file, "$..id_requisito") YIELD value AS result
with json_file, result.result AS keys
return keys

This was the first one and the result I got was this:

imagen

The other way is this:

WITH 'recursivo.json' AS json_file
CALL apoc.load.json(json_file) YIELD value AS result
with json_file, result.id_requisito AS keys
return keys

And the result:

imagen

If you want to know what's the JSON file:

[
{
	"id_requisito" : "R1",
	"sentence" : {
		"operator": "&&",
		"left" : "H",
		"right" : {
			"operator" : "&&",
			"left" : "A",
			"right" : {
				"operator" : "||",
				"left" : "B",
				"right" : "C"
			}
		}
	}
},
{
	"id_requisito" : "R2",
	"sentence" : {
		"operator": "||",
		"left" : "A",
		"right" : "B"
	}
},

{
	"id_requisito" : "R3",
	"sentence" : {
		"operator": "&&",
		"left" : {
			"operator" : "||",
			"left" : "A",
			"right" : "B"
		},
		"right" : "C"
	}
}


]

It would be possible to remove the null values or create a list of strings?

Thank you.

You should be able to do something like:

WITH 'recursivo.json' AS json_file
CALL apoc.load.json(json_file, "$..id_requisito") YIELD value AS result
with json_file, result.result AS keys
WHERE keys is not null
return keys

or maybe:

CALL apoc.load.json(json_file, "$..id_requisito") YIELD value AS result
with json_file, result.result AS keys
unwind keys as k
where k is not null
return keys

Depending on how you want the output and how it works out.

Hi Michael,

I've tried both of them and the first one has this result:

imagen

The second one returns an error:

Thank you for your answer.

It sounds like it would be beneficial if you could pre-process the data to remove nulls. I'm not sure if you're getting this from an API or some other source but cleaning your data is always the largest part of the battle.

You can try this code

WITH 'recursivo.json' AS json_file
CALL apoc.load.json(json_file, "$..id_requisito") YIELD value AS result
WITH json_file, [x in result.result  WHERE x is not null | x] as AS keys
return keys
2 Likes

You got it, thank you
imagen