apoc.convert.getJsonPropertyMap

Function APOC Core

apoc.convert.getJsonPropertyMap(node,key[,'json-path', 'path-options']) - converts serialized JSON in property back to map

Signature

apoc.convert.getJsonPropertyMap(node :: NODE?, key :: STRING?, path =  :: STRING?, pathOptions = null :: LIST? OF STRING?) :: (MAP?)

Input parameters

Name Type Default

node

NODE?

null

key

STRING?

null

path

STRING?

pathOptions

LIST? OF STRING?

[]

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (:Person {json:'{a:[1,2,3]}'});
MATCH (p:Person)
RETURN apoc.convert.getJsonPropertyMap(p, "json") AS output;
Table 1. Results
Output

{a: [1, 2, 3]}

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, with a (n:JsonPathNode {prop: '{"columns":{"col2":{"_id":"772col2"}}}'}) we can execute (with default pathOptions):

MATCH (n:JsonPathNode) RETURN apoc.convert.getJsonPropertyMap(n, 'prop', '$.columns.col2') AS output;
Table 2. Results
output

{ "_id": "772col2" }

or, with custom path options:

MATCH (n:JsonPathNode) RETURN apoc.convert.getJsonPropertyMap(n, 'prop', '$.columns.col2', ['ALWAYS_RETURN_LIST']) AS path
Table 3. 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]

Also, we could leverage the apoc.json.validate procedure (provided with APOC full) to handle incorrectly formatted JSONs by continuing the execution instead of causing it to fail. For example, given a node (:JsonInvalidNode {prop: '{"foo": [{"baz": 18446744062065078016838}],"baz": 7}'}), the following query returns 0 instead of failing due to InputCoercionException: Numeric value (18446744062065078016838) out of range:

MATCH (n:JsonInvalidNode) WITH n
CALL { WITH n CALL apoc.json.validate(n.prop) YIELD value RETURN collect(value) AS errs }
RETURN CASE errs WHEN [] THEN apoc.convert.getJsonPropertyMap(n, 'prop', '$',  []) ELSE 0 END AS value