Skip to content

JSON¤

SP-Lang offers a high-speed access to JSON data objects.


!GET: Get the value from a JSON¤

Type: Mapping.

Synopsis:

!GET
what: <item>
type: <type>
from: <json>
default: <value>

Get the item specified by the what from the from JSON object. If the item is not found, return default or error if default is not provided. default is optional.

You may optionally specify the item type by type.

Example

JSON (aka !ARG jsonmessage):

{
"foo.bar": "Example"
}

Get the field foo.bar from a JSON above:

!GET
what: foo.bar
from: !ARG jsonmessage

JSON Pointer¤

If you want to access the item in the nested JSON, you need to use a JSON Pointer (e.g. /foo/bar o/foo/bar) as a what for that.

The schema will be applied to infer the type of the item but for more complex access, the type argument is recommended.

Example

Nested JSON (aka !ARG jsonmessage):

{
"foo": {
    "bar": "Example"
}
}

Example of extraction of the string from the nested JSON:

!GET
what: /foo/bar
type: str
from: !ARG jsonmessage

!JSON.PARSE: Parse JSON¤

Type: Mapping.

Synopsis:

!JSON.PARSE
what: <str>
schema: <schema>

Parse JSON string. The result can be used with e.g. !GET operator.

Optional argument schema specifies the schema to be applied. The default schema is build-in ANY.

Example

!JSON.PARSE
what: |
{
    "string1": "Hello World!",
    "string2": "Goodby ..."
}