New in version 6.1.0: See the new features.

Read a web stdin JSON content

read_web_json_values()

Read a web stdin JSON content into a !record


SYNTAX

read_web_json_values(recordId [, options])

This method is only available in nph-srep.

Read into the record the received stdin JSON content (if nph-srep is executed from a web server the content is received from the client “browser” as the message content in a PUT or POST method)

The content is converted from utf8 to the system native enconding (see note)

recordId

Record where to read the JSON values into

options

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

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 (see read_json_values example)

  • 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.

  • 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.


EXAMPLE

This nph-srep example reads a JSON or an XML request depending on the CONTENT_TYPE received:

!record postRequest {
    testI4,,i4
    testR8,,r8
    testA32,,a32,,v
}

/* Check the correct post method */
if (get_web_method() <> METHOD_POST) {
    web_status(405, "METHOD NOT SUPPORTED")
    end
}

/* Get the request values */
switch (getenv("CONTENT_TYPE")) {
case = "application/json":
    read_web_json_values(postRequest)
    break

case = "application/xml":
    read_web_xml_values(postRequest)
    break

default:
    web_status(415, "MEDIA TYPE NOT SUPPORTED")
    put "Server does not support media type"
    end
}
...

RELATED TOPICS

Creating web pages with Sculptor

Internet functions

print_json

JSON and XML functions

!record

sys.CharSetEncodeFrom and sys.CharSetEncodeTo