Xbasic

json_select Function

Syntax

C result = json_select(jsonText as C,columns as C [,primaryKey as C [,CRC as L [,flags as C ]]])

Arguments

jsonTextCharacter

Text to select columns from.

columnsCharacter

Comma separated list of columns to select

primaryKeyCharacter

Optional primary key - allows for multiple columns to be pasted together in a single column (separated by commas or pipes).

CRCLogical

Checksum value for data, provides a simple way to detect if a row has changed.

flagsCharacter

Flags to control selection output.

Flag
Description
N

Generate as objects (default behaviour is to output an array).

S

Stringize the contents (numeric and boolean values are quoted).

T

Trim the key value (remove leading and trailing spaces).

U

Enforce uniqueness on selected JSON data.

Description

Generates a string containing a Javascript array with data from the specified columns of a JSON object.

Example

dim json as c 
json = <<%str%
[
    {"firstname":"John","lastname":"Smith","city":"Boston","state":"MA"},
    {"firstname":"Fred","lastname":"Jones","city":"Cambridge","state":"MA"},
    {"firstname":"Tom","lastname":"King","city":"New York","state":"NY"}
]
%str%
result = json_select(json,"firstname,lastname")
? result
= [ [ "John" , "Smith" ]
, [ "Fred" , "Jones" ]
, [ "Tom" , "King" ]
 ]

The 'N' option allows for names to be preserved, use JSON_SELECT to limit columns, keep the object format.

dim json as c 
json = <<%str%
[
    {"firstname":"John","lastname":"Smith","city":"Boston","state":"MA"},
    {"firstname":"Fred","lastname":"Jones","city":"Cambridge","state":"MA"},
    {"firstname":"Tom","lastname":"King","city":"New York","state":"NY"}
]
%str%
? json_select(json,"firstname,lastname","",.f.,"n")
= [ { "firstname" : "John" , "lastname" : "Smith" }
, { "firstname" : "Fred" , "lastname" : "Jones" }
, { "firstname" : "Tom" , "lastname" : "King" }
 ]

See Also