New in version 6.1.0: See the new features.

Read JSON

read_json_values()

Read the JSON content into a !record


SYNTAX

read_json_values(filename|channel|string, recordId [, options])

That method will open the requested file or channel and read all the JSON key/value pairs setting the values on the fields with name equal to the key.

filename

JSON file to read.

channel

Since version 6.2.0 a channel can be specified to read a JSON (open sequential file, pipe, …).

string

JSON in memory string when the option JSONXML_MEMORY is specified.

recordId

Record where to read the JSON values into.

options

JSONXML_MINIFY to remove comments, if they exist, that make the parser fail.

Since Sculptor version 6.2.0 JSONXML_MEMORY specifies that the first parameter is a JSON string in memory (probably an alpha packed field).

It returns the number of keys read from the JSON content (0 if none)

Note

New in version 6.2.0: See the new features.

  • Nested objects are supported since the nested record feature.

  • Array of objects are supported since the array record feature.

  • The field arrays can be autoredim’ed if the “a” flag is set.

  • The !record arrays can be autoredim’ed if the RECFL_AUTOREDIM flag is set.

  • Sequential files can be specified in place of a file name.

  • To address the problem of reading/writting a json/xml tag which happens to be a Sculptor reserved word (creating a field on a record with that name is not possible). It’s possible to define the reserved word with an underscore “_” in front of it. Not clashing with the reserved word any more. As a rule, when writting/reading the first “_” will be removed (if an underscore is needed just add one more). E.g:

    !record Rec {
        _image,,a64
    }
    
  • Now the function treats an ASCII or binary array with a “p” (packed flag) flag as one big field, so that large JSON fields can be read. E.g.:

    !record Rec {
        large8kDataString,,a128[64],,p
    }
    

New in version 6.3.0: See the new features.

The character conversion honors the value set in sys.CharSetEncodeFrom and sys.CharSetEncodeTo, so that the string received is supposed to be sys.CharSetEncodeTo encoded while the result is converted into the sys.CharSetEncodeFrom encoding.


EXAMPLES

A JSON file (e.g “file.json”) with this content:

{
    "boolVal": true,
    "intVal": 123,
    "realVal": 123.123,
    "strVal": "Json String",
    "arrIntsVal": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ,0 ]
}

Can be read with this Sculptor code:

!record infoJson {
    boolVal,,u1
    intVal,,i4
    realVal,,r8
    strVal,,a128
    arrIntsVal,,i4[10]
}

num = read_json_values("file.json", infoJson)
prompt "Read " + tostr(num) + " Json attributes"

Since Sculptor version 6.2.0 this JSON content pipe’ed:

[{
    "pre":  1234,
    "txt":  "Top     ",
    "SubRec":  {
        "num":  [1111, 2121],
        "txt":  "SubRec  "
    },
    "num":  [1123, 2123],
    "SubRec2":  {
        "num":  [1010, 2020],
        "txt":  "SubRec2 "
    },
    "post":  225
}, {
    "pre":  2234,
    "txt":  "Top2    ",
    "SubRec":  {
        "num":  [2111, 2112],
        "txt":  "2ubRec1 "
    },
    "num":  [1122, 2122],
    "SubRec2":  {
        "num":  [2211, 2212],
        "txt":  "2ubRec21"
    },
    "post":  226
}]

Can be read with this Sculptor code:

!record SrcRec[1] {
    pre,,i2
    txt,,a8
    !record SubRec {
        num,,i4[2]
        txt,,a8
    }
    num,,i4[2]
    !record SubRec2 = SrcRec.SubRec
    post,,i2
}

SrcRec->flags = RECFL_AUTOREDIM

/* Open channel 1 as stdin */
open #1, "stdin" read

read_json_values(1, SrcRec, JSONXML_MINIFY)
/* The SrcRec first dimension is automatically redim'ed from 1 to 2 and the JSON is read */

RELATED TOPICS

write_json_values

!record

Sequential files

sys.CharSetEncodeFrom and sys.CharSetEncodeTo