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.

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:

2X_3_3cb7ee95a189ac40ad5d36e05e696b07aa51bd17.png

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:

2X_6_6f08c7d376ef8a0ccae82e9189f1fbe2c9550d93.png

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.

1 ACCEPTED SOLUTION

anthapu
Graph Fellow

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

View solution in original post

5 REPLIES 5

MuddyBootsCode
Graph Steward

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:

2X_5_56082315140aebab74aa25fbb4374c2f6406e8fa.png

The second one returns an error:

Thank you for your answer.

MuddyBootsCode
Graph Steward

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.

anthapu
Graph Fellow

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

You got it, thank you
2X_2_2d6a979f0d3d9e68d292a25c9f86751553ae0de0.png