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.

Can I add jsonObject as a value to a property in a node?

Hi, I'm trying to add json object to a property in a node. I cannot see any apoc functions for that. Is there a way? I tried using some functions like convert.toJson, but none of them work.

Thank you

5 REPLIES 5

You an add a JSON string as a property to a node, but a JSON structure is not supported.

At this time besides primitive types we allow lists of primitive types, but not more complex lists (no nested lists, no lists of maps) and no map properties.

While you could store this as a JSON string (and there are APOC functions to convert JSON structures to strings if needed), it won't be searchable via indexes, you would need to get to a node, convert it back to a structure (via APOC) then explore it / filter from there.

You might also consider if it's possible to model the JSON within the graph, as separate nodes with relationships to each other as needed.

I have a couple of questions

  1. What is a json string? What's the format/syntax?
  2. The properties I consider to have as json are "telephone numbers", "addresses" and "emails". I would like a user to have the option to add multiple. If I model these within the graph I will end up having too many nodes which are not really useful since these properties are just informational and will not be used for searching etc. Considering this, is it still a good idea in my case to model these within the graph?

JSON is a way to represent a complex data structure that can have objects (key/value maps) and lists, and these can be nested.

When working at the code level, we're usually working with the structure itself, and we can explore the structure, iterating through list values and exploring object properties by their key.

When transporting it across the wire or between systems, we usually render it as a string, a textual representation. In a string form it is not a structure, you cannot explore it or make changes to it as if it was a structure.

So when we're talking about saving it as a JSON string, that's a single property on the node where the text representation of the JSON will be stored. To work with it in any reasonable way (aside from just returning the entire thing) you would need to convert this back to a structure (of lists and maps) that can be explored. APOC Procedures offers some conversion functions (apoc.convert.fromJsonList() and apoc.convert.fromJsonMap()) to change the string back into the full data structure.

That said, what you're describing, a JSON with telephone numbers, addresses, and emails, these sound like node properties. A node's properties are like a simple JSON structure, though map properties and lists of maps aren't allowed, nor are lists of lists.

So you may be able to represent what you want with something like a :User node with list properties for phoneNumbers, addresses, and emails. It does help that you won't be doing lookups with these values.

Once you have the map structure, you can set it all at once as a node's properties (using the + operator to replace the node's properties, or the += operator to add to its existing properties).

If you need further help, then we may need some concrete examples from you as to the data structure you're trying to save.

Hello
I have a network topology which consists of a number of switches and hosts.
Using Neo4j, I created a graph db that import the network topology information from a json file. and it works.
Now I need to store the switch statistics like the no. of packet sent, packet received, packet loss for a specific period of time. The json file consists of a huge no. of lines (more than 2000 line) for only one switch.
I face a problem storing this kind of data as node properties since it is huge data!
Any suggestions please. Does Neo4j can benefit me in storing this kind of data?
Many thanks
Note: I upload a sample file for the data.
response_4.txt (36.9 KB)

clem
Graph Steward

I think if you look at the python JSON library, it will be clearer.

See: json — JSON encoder and decoder — Python 3.9.5 documentation

So, if you have a JSON object, you can convert it into a string (using json.dumps()) that can be later converted back into a JSON object (using json.loads()) .

Stealing from the documentation:

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'

and to convert it back:

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]

Admittedly, it's a bit of a hack, but you do what you got to do...