Xbasic

json_sample_array Function

Syntax

C result = json_sample_array(jsonText as C[, format as C[,limit as N]])

Arguments

jsonTextCharacter

JSON to enumerate.

formatCharacter

Optional formatting directives that format the output. Format can include the following directives:

Option
Description
$n

name

$i

case insensitive name,

$t

type (C-charcter/N-number/L-logical/Z-null/?-unknown)

$#t

type looking for stringized time and date as well (T-time/D-date...)

$c

count - occurences of field,

$m

records sampled,

$s

field is sparse (true or false).

$r

Recurse into child objects.

$a

Recurse into array objects, adding [] suffix.

$o

Recurse into objects, adding {} suffix.

$w

Maximum sampled data width.

$d

Maximum sampled decimal places.

limitNumeric

Description

Enumerate top level properties in array entries and return name + count.

Uses format spec parameters. Note that you can combine specs - i.e. $ao for arrays and objects, $ar for arrays and recursion.

Examples

dim json as c 
json = <<%json%
[
	{"Firstname" : "Fred", "Lastname" : "Smith"},
	{"Firstname" : "Harry", "Lastname" : "Jones","age" : 30},
]
%json%
? json_sample_array(json)
= Firstname
Lastname
age

? json_sample_array(json,"$n - $t")
= Firstname - C
Lastname - C
age - N

Example showing recursion into name.

dim json as c 
json = <<%json%
[
	{"Firstname" : "Fred", "Lastname" : "Smith" , "address" : { "city" : "Boston" , "state" : "ma" } },
	{"Firstname" : "Harry", "Lastname" : "Jones","age" : 30},
]
%json%
? json_sample_array(json,"$r")
= Firstname
Lastname
address.city
address.state
age

Example showing recursion into arrays and objects.

json = <<%json%
[
    {
        "Firstname": "John",
        "Lastname": "Smith",
        "City": "Boston",
        "State": "MA",
        "Company": {
            "name": "alpha",
            "software": true
        },
        "children": [
            {
                "name": "Jill"
            },
            {
                "name": "Jack"
            }
        ]
    }
]
%json%

dim newField as c = json_sample_array(json,"$ao|$s")

? newField
= Firstname|false
Lastname|false
City|false
State|false
Company{}
Company{}.name|false
Company{}.software|false
children[]
children[].name|false

See Also