PPL JSON functions - Amazon OpenSearch Service

PPL JSON functions

Note

To see which AWS data source integrations support this PPL function, see Functions.

JSON

Usage: json(value) evaluates whether a string can be parsed as JSON format. The function returns the original string if it's valid JSON, or null if it's invalid.

Argument type: STRING

Return type: STRING/NULL. A STRING expression of a valid JSON object format.

Examples:

os> source=people | eval `valid_json()` = json('[1,2,3,{"f1":1,"f2":[5,6]},4]') | fields valid_json fetched rows / total rows = 1/1 +---------------------------------+ | valid_json | +---------------------------------+ | [1,2,3,{"f1":1,"f2":[5,6]},4] | +---------------------------------+ os> source=people | eval `invalid_json()` = json('{"invalid": "json"') | fields invalid_json fetched rows / total rows = 1/1 +----------------+ | invalid_json | +----------------+ | null | +----------------+

JSON_OBJECT

Usage: json_object(<key>, <value>[, <key>, <value>]...) returns a JSON object from members of key-value pairs.

Argument type:

  • A <key> must be STRING.

  • A <value> can be any data types.

Return type: JSON_OBJECT. A StructType expression of a valid JSON object.

Examples:

os> source=people | eval result = json_object('key', 123.45) | fields result fetched rows / total rows = 1/1 +------------------+ | result | +------------------+ | {"key":123.45} | +------------------+ os> source=people | eval result = json_object('outer', json_object('inner', 123.45)) | fields result fetched rows / total rows = 1/1 +------------------------------+ | result | +------------------------------+ | {"outer":{"inner":123.45}} | +------------------------------+

JSON_ARRAY

Usage: json_array(<value>...) creates a JSON ARRAY using a list of values.

Argument type: A <value> can be any kind of value such as string, number, or boolean.

Return type: ARRAY. An array of any supported data type for a valid JSON array.

Examples:

os> source=people | eval `json_array` = json_array(1, 2, 0, -1, 1.1, -0.11) fetched rows / total rows = 1/1 +------------------------------+ | json_array | +------------------------------+ | [1.0,2.0,0.0,-1.0,1.1,-0.11] | +------------------------------+ os> source=people | eval `json_array_object` = json_object("array", json_array(1, 2, 0, -1, 1.1, -0.11)) fetched rows / total rows = 1/1 +----------------------------------------+ | json_array_object | +----------------------------------------+ | {"array":[1.0,2.0,0.0,-1.0,1.1,-0.11]} | +----------------------------------------+

TO_JSON_STRING

Usage: to_json_string(jsonObject) returns a JSON string with a given json object value.

Argument type: JSON_OBJECT

Return type: STRING

Examples:

os> source=people | eval `json_string` = to_json_string(json_array(1, 2, 0, -1, 1.1, -0.11)) | fields json_string fetched rows / total rows = 1/1 +--------------------------------+ | json_string | +--------------------------------+ | [1.0,2.0,0.0,-1.0,1.1,-0.11] | +--------------------------------+ os> source=people | eval `json_string` = to_json_string(json_object('key', 123.45)) | fields json_string fetched rows / total rows = 1/1 +-----------------+ | json_string | +-----------------+ | {'key', 123.45} | +-----------------+

ARRAY_LENGTH

Usage: array_length(jsonArray) returns the number of elements in the outermost array.

Argument type: ARRAY. An ARRAY or JSON_ARRAY object.

Return type: INTEGER

Example:

os> source=people | eval `json_array` = json_array_length(json_array(1,2,3,4)), `empty_array` = json_array_length(json_array()) fetched rows / total rows = 1/1 +--------------+---------------+ | json_array | empty_array | +--------------+---------------+ | 4 | 0 | +--------------+---------------+

JSON_EXTRACT

Usage: json_extract(jsonStr, path) extracts a JSON object from a JSON string based on the specified JSON path. The function returns null if the input JSON string is invalid.

Argument type: STRING, STRING

Return type: STRING

  • A STRING expression of a valid JSON object format.

  • NULL is returned in case of an invalid JSON.

Examples:

os> source=people | eval `json_extract('{"a":"b"}', '$.a')` = json_extract('{"a":"b"}', '$a') fetched rows / total rows = 1/1 +----------------------------------+ | json_extract('{"a":"b"}', 'a') | +----------------------------------+ | b | +----------------------------------+ os> source=people | eval `json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[1].b')` = json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[1].b') fetched rows / total rows = 1/1 +-----------------------------------------------------------+ | json_extract('{"a":[{"b":1.0},{"b":2.0}]}', '$.a[1].b') | +-----------------------------------------------------------+ | 2.0 | +-----------------------------------------------------------+ os> source=people | eval `json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[*].b')` = json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[*].b') fetched rows / total rows = 1/1 +-----------------------------------------------------------+ | json_extract('{"a":[{"b":1.0},{"b":2.0}]}', '$.a[*].b') | +-----------------------------------------------------------+ | [1.0,2.0] | +-----------------------------------------------------------+ os> source=people | eval `invalid_json` = json_extract('{"invalid": "json"') fetched rows / total rows = 1/1 +----------------+ | invalid_json | +----------------+ | null | +----------------+

JSON_KEYS

Usage: json_keys(jsonStr) returns all the keys of the outermost JSON object as an array.

Argument type: STRING. A STRING expression of a valid JSON object format.

Return type: ARRAY[STRING]. The function returns NULL for any other valid JSON string, an empty string, or an invalid JSON.

Examples:

os> source=people | eval `keys` = json_keys('{"f1":"abc","f2":{"f3":"a","f4":"b"}}') fetched rows / total rows = 1/1 +------------+ | keus | +------------+ | [f1, f2] | +------------+ os> source=people | eval `keys` = json_keys('[1,2,3,{"f1":1,"f2":[5,6]},4]') fetched rows / total rows = 1/1 +--------+ | keys | +--------+ | null | +--------+

JSON_VALID

Usage: json_valid(jsonStr) evaluates whether a JSON string uses valid JSON syntax and returns TRUE or FALSE.

Argument type: STRING

Return type: BOOLEAN

Examples:

os> source=people | eval `valid_json` = json_valid('[1,2,3,4]'), `invalid_json` = json_valid('{"invalid": "json"') | feilds `valid_json`, `invalid_json` fetched rows / total rows = 1/1 +--------------+----------------+ | valid_json | invalid_json | +--------------+----------------+ | True | False | +--------------+----------------+ os> source=accounts | where json_valid('[1,2,3,4]') and isnull(email) | fields account_number, email fetched rows / total rows = 1/1 +------------------+---------+ | account_number | email | |------------------+---------| | 13 | null | +------------------+---------+