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

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.

1 Like