New in version 6.1.0: See the new features.

Write JSON

write_json_values()

Write the JSON content from a !record


SYNTAX

write_json_values(filename|channel|field, recordId [, options])

That method will create the requested content and write all the JSON key/value pairs from the values of the record fields where the key is the field name and the value it’s content.

filename

JSON file to write

channel

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

field

Field to receive the JSON string in memory when the option JSONXML_MEMORY is specified.

recordId

Record where to write the JSON values into

options

JSONXML_MINIFY removes all unnecessary spaces and characters to reduce the transfered size.

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

It returns the number of keys written into the JSON file or channel (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.

  • 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 written. 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.CharSetEncodeFrom encoded while the result is converted into the sys.CharSetEncodeTo encoding.


EXAMPLE

This code writes the !record content into a file as JSON:

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

for (num=1; num <= 10; num++) {
    infoJson.arrIntsVal[num] = num
}
infoJson.boolVal = TRUE
infoJson.intVal = 543
infoJson.realVal = 543.43
infoJson.strVal = "Writting JSON"

num = write_json_values("file.json", infoJson)
prompt "Written " + tostr(num) + " Json attributes"

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

{
    "boolVal":      1,
    "intVal":       543,
    "realVal":      543.430000,
    "strVal":       "Writting JSON",
    "arrIntsVal":   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

Since Sculptor version 6.2.0 this JSON:

{
"pre":  1234,
"txt":  "Top     ",
"SubRec":  {
    "num":  [1111, 2121],
    "txt":  "SubRec  "
},
"num":  [1123, 2123],
"SubRec2":  {
    "num":  [1010, 2020],
    "txt":  "SubRec2 "
},
"post":  225
}

Can be written into a pipe with this Sculptor code:

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

/* Fill SrcRec with information */
...

/* Execute a background "cmd" program using channel 1 as their stdin (used here as stdout) */
execute(NULL, cmd, NULL, 0, AA_SHOWNORMAL, 1, 0, 0)

/* Send the SrcRec in JSON format to the "cmd" program */
write_json_values(1, SrcRec, JSONXML_MINIFY)

RELATED TOPICS

read_json_values

!record

Sequential files

JSONXML_ options

sys.CharSetEncodeFrom and sys.CharSetEncodeTo