How to store a map or json object as a property in neo4j?

That's a limitation of node properties right now. You have a few workarounds to choose from.

  1. You can turn your json object into a string and save it as a property. You can use APOC Procedures to convert JSON strings to and from Cypher map objects.

  2. You can instead save the map properties as properties on the node, though that loses the grouping you would get from the object itself.

  3. If #2 isn't enough, you could also alter your graph model so the data in the JSON object corresponds to graph objects, nodes and properties related to the original node.


Storing a map as properties is supported by neo java OGM library. The library will unwind your map to graph node properties if you annotate the java class field with @Properties.

Class:

@NodeEntity
public class DataNode {

    @GraphId
    private Long id;

    @Properties
    private Map<String, String> map = new HashMap<>();

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

How to use in java:

    DataNode data = new DataNode();
    data.getMap().put("testKey", "keyvalue");
    data.getMap().put("myKey", "key value for my key");

How it will look in neo:

enter image description here

Notice that if you end up using map keys with dot ".", you might get funky side effects when loading the entity from neo => nested maps. Consider then changing the delimiter in @Properties or change the key.

Tags:

Neo4J

Cypher