JSON_GET_TYPE

Use the JSON_GET_TYPE script function to get the data type of a JSON element.

Syntax

JSON_GET_TYPE (JSON, JSONPath)

Parameters

  • JSON
    A string containing a valid JSON document

  • JSONPath
    The path to the JSON element that should be checked. Only one element can be selected at a time.

Return value: String, one of the following items:

  • string
  • boolean
  • number
  • object
  • array
  • null

Runtime errors

The function will cause a runtime error under the following circumstances:

  • 45334
    If your JSON document is invalid
  • 45342
    Syntax Error in JSONPath expression
  • 45339
    JSONPath matches more than one element
  • 45340
    JSONPath does not match any element

Example

The following example checks the type of various elements in a JSON document.

:SET &JSON_DOC# ='{"host":"192.168.0.10","active":false, "itemNumber": 2, "bootSequence":["hdd02"]}'

!&STRING_TYPE# is 'string'
:SET &STRING_TYPE# = JSON_GET_TYPE( &JSON_DOC#, '$.host')
:P &STRING_TYPE#

!&BOOLEAN_TYPE# is 'boolean'
:SET &BOOLEAN_TYPE# = JSON_GET_TYPE( &JSON_DOC#, '$.active')
:P &BOOLEAN_TYPE#

!&NUM_TYPE# is 'number'
:SET &NUM_TYPE# = JSON_GET_TYPE( &JSON_DOC#, '$.itemNumber')
:P &NUM_TYPE#

!&ARR_TYPE# is 'array'...
:SET &ARR_TYPE# = JSON_GET_TYPE( &JSON_DOC#, '$.bootSequence')
:P &ARR_TYPE#

! ... but &STR_ARR_TYPE# is 'string', because we access the first item of the array
:SET &STR_ARR_TYPE# = JSON_GET_TYPE( &JSON_DOC#, '$.bootSequence[0]')
:P &STR_ARR_TYPE#

Activation report result:

2019-05-13 10:03:24 - U00020408 string
2019-05-13 10:03:24 - U00020408 boolean
2019-05-13 10:03:24 - U00020408 number
2019-05-13 10:03:24 - U00020408 array
2019-05-13 10:03:24 - U00020408 string

See also: