All Products
Search
Document Center

CDN:JSON functions

Last Updated:Jun 04, 2024

This topic describes the syntax, features, parameters, and return values of JSON functions. This topic also provides examples of these functions.

json_enc

The following table describes the details about this function.

Item

Description

Syntax

json_enc(d)

Description

Encodes a dictionary object into a JSON string.

Parameters

d: the dictionary object that you want to encode.

Return value

If the function succeeds, a JSON-encoded string is returned. Otherwise, false is returned.

Example

var_a = []
var_b = ['v1', 'v2']
set(var_a, 'k1', 'v1')
set(var_a, 'k2', var_b)
var_c = '{"k1":"v1","k2":["v1","v2"]}'
say(concat('json_enc=', json_enc(var_a)))
say(concat('json_dec=', get(json_dec(var_c), 'k1')))

Output:
json_enc={"k1":"v1","k2":["v1","v2"]}
json_dec=v1

json_dec

The following table describes the details about this function.

Item

Description

Syntax

json_dec(s)

Description

Decodes a JSON string into a dictionary.

Parameters

s: the JSON string that you want to decode.

Return value

If the function succeeds, a dictionary is returned. Otherwise, false is returned.

Note

A numeric string such as "123" can also be decoded into a variable of the number type. If you want to perform operations on the returned dictionary, such as getting a value, use the type function to determine the type of the variable.

Example

var_c = '123'
type_var_c = type(json_dec(var_c))
if eq(type_var_c, 'table') {
  say(concat('json_dec=', get(json_dec(var_c), 'k1')))
} else {
  say(type_var_c)
}
var_c = '{"k1":"v1","k2":["v1","v2"]}'
type_var_c = type(json_dec(var_c))
if eq(type_var_c, 'table') {
  say(concat('json_dec=', get(json_dec(var_c), 'k1')))
} else {
  say(type_var_c)
}

Output:
number
json_dec=v1