apoc.convert.fromJsonMap

Details

Syntax

apoc.convert.fromJsonMap(map [, path, pathOptions ])

Description

Converts the given JSON map into a Cypher MAP.

Arguments

Name

Type

Description

map

STRING

A JSON stringified map.

path

STRING

A JSON path expression used to extract a certain part from the map. The default is: ``.

pathOptions

LIST<STRING>

JSON path options: ('ALWAYS_RETURN_LIST', 'AS_PATH_LIST', 'DEFAULT_PATH_LEAF_TO_NULL', 'REQUIRE_PROPERTIES', 'SUPPRESS_EXCEPTIONS') The default is: null.

Returns

MAP

Usage examples

The following converts a JSON map into a Cypher map:

RETURN apoc.convert.fromJsonMap('{"name": "Graph Data Science Library"}') AS output;
Results
Output

{name: "Graph Data Science Library"}

We can also use JSON path expressions to extract part of a JSON map. For example, the following extracts the product property from a JSON map and returns a map:

RETURN apoc.convert.fromJsonMap('{"product": {"name": "Bloom"}}', '$.product') AS output;
Results
Output

{name: "Bloom"}

If we try to convert a non-map structure, we’ll get an exception. For example:

RETURN apoc.convert.fromJsonMap('[{"name": "Neo4j"}]') AS output;
Results

Failed to invoke function apoc.convert.fromJsonMap: Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.Object,java.lang.Object> out of START_ARRAY token at [Source: (String)"[{"name": "Neo4j"}]"; line: 1, column: 1]

In this case we should instead use apoc.convert.fromJsonList.

Moreover, we can customize the Json path options, adding as third parameter (pathOptions) a list of strings, where the strings are based on Enum<Option>. The default value is ["SUPPRESS_EXCEPTIONS", "DEFAULT_PATH_LEAF_TO_NULL"]. Note that we can also insert [], that is "without options". So we can execute (with default pathOptions):

RETURN apoc.convert.fromJsonMap('{ "columns": {
      "col2": {
        "_id": "772col2"
      }
    }
}', '$.columns.col2') AS output;
Results
output

{ "_id": "772col2" }

or, with custom path options:

RETURN apoc.convert.fromJsonMap('{ "columns": {
      "col2": {
        "_id": "772col2"
      }
    }
}', '$.columns.col2', ['ALWAYS_RETURN_LIST']) AS output;
Results
Failed to invoke function apoc.convert.getJsonPropertyMap: Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.Object,java.lang.Object> out of START_ARRAY token at [Source: UNKNOWN; line: -1, column: -1]