parse_json

Description

Parse the argument value as a JSON string.

The object or array that comes out is typically put into a column using make_col.

parse_json accepts one option:

allow_duplicate_keys determines how OPAL handles JSONs with duplicate keys. By default, on JSON inputs with duplicate keys, the function will return JSON objects containing duplicate keys’ last value. If disabled, OPAL will return null for these inputs.

Return type

variant

Domain

This is a scalar function (calculates a single output value for a single input row.)

Categories

Usage

parse_json(value, [ options ])

Argument

Type

Optional

Repeatable

Restrictions

value

string

no

no

none

options

options

yes

no

constant

Options

Option

Type

Meaning

allow_duplicate_keys

bool

Applies to JSON inputs with duplicate keys. If true, the last value of the duplicate key is used. If false, the function returns null. Default is false.

Examples

make_col json:parse_json(string(payload))

Make a new column, of type variant, containing the contents of field payload parsed as JSON.

make_col json:object(parse_json('{ "k1": "v1", "k2": [ "one", "two" ], "k3": { "key 4": ["five"] } }'))

Make a new column containing the specified string parsed as JSON. The result is coerced to object to ensure it has the desired type.

extract_regex log, /JSON payload: (?P<StringPayload>{.*})/
make_col payload:parse_json(StringPayload)

Extract the JSON portion of the string log into new field StringPayload, then parse it as JSON.

make_col json:parse_json('{ "k1": "v1", "k1": "v2" }', options(allow_duplicate_keys: false))

Make a new column containing the specified string parsed as JSON. By default, because parse_json allows duplicate keys, the column would contain null. However, since allow_duplicate_keys is enabled, the column will contain {"k1": "v2"}, which is the latest value of the duplicate key.

Aliases

parsejson (deprecated)