Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j/cypher: inline conversion of string to a map

Tags:

neo4j

cypher

this might be obvious, but not to me: I have a JSON object has a property that contains stringified JSON (don't ask why, it just has to be that way) when I UNWIND the object and grab that property (key) is there a way to convert to a map inline to the cipher query?

WITH {input} AS S
UNWIND S AS stat
WITH stat.key.str as K, stat.value as value
UNWIND K as key
RETURN key, value

returns:

key                                                                          value    
-----------------------------------------------------------------------------------------
{"role":"consumer","ipproto":"TCP","l7proto":"tcp:80","port":80,"dir":""} 156
{"role":"consumer","ipproto":"TCP","l7proto":"tcp:443","port":443,"dir":""} 223
{"role":"consumer","ipproto":"TCP","l7proto":"MSN","port":1863,"dir":""} 106208

is it possible to parse/load the string value in key to read it as a map or do I have to convert it outside the cypher query?

apologies - I am noob to both neo4j and cypher...

like image 902
timHughes Avatar asked Oct 23 '25 17:10

timHughes


1 Answers

You'll need APOC Procedures for this, specifically apoc.convert.fromJsonMap() to convert it to a Cypher map. Here's how you would use it:

WITH {input} AS S
UNWIND S AS stat
WITH stat.key.str as K, stat.value as value
UNWIND K as key
WITH apoc.convert.fromJsonMap(key) as map, value
...
like image 156
InverseFalcon Avatar answered Oct 26 '25 04:10

InverseFalcon