util.JSON.proposeType

Describes the record structure that can hold a given JSON data string.

Syntax

util.JSON.proposeType(
   s STRING )
  RETURNS STRING
  1. s is a string value that contains JSON formatted data.

Usage

The util.JSON.proposeType() class method takes a JSON formatted string as parameter and generates the RECORD definition that can be used as base record definition hold the source JSON string.

If the provided string is not valid JSON, the proposeType() method will raise error -8109. Consider enclosing the proposeType() method call in a TRY/CATCH block, if the source string can be malformed JSON.

In order to respect the BDL syntax, and match exactly the JSON source structure, the type generated by proposeType() may need to be reviewed.

For example, if the JSON property name contains characters not allowed in a BDL identifiers, use the json_name variable definition attribute to map the JSON property name to the RECORD element. Furthermore, the type DICTIONARY will not be proposed. As result, a source JSON string like {"First Name":"John","Last Name":"Piper"} will produce:
RECORD
    First Name STRING,
    Last Name STRING
END RECORD
That needs to be adapted to use json_name attributes:
RECORD
    first_name STRING ATTRIBUTES(json_name="First Name"),
    last_name  STRING ATTRIBUTES(json_name="Last Name")
END RECORD
Or to a DICTIONARY type:
DICTIONARY OF STRING

Example

IMPORT util
MAIN
    DEFINE js STRING
    LET js='{ "cust_num":2735, "cust_name":"McCarlson",
              "orderids":[234,3456,24656,34561] }'
    DISPLAY util.JSON.proposeType( js )
END MAIN

Displays:

RECORD
    cust_num FLOAT,
    cust_name STRING,
    orderids DYNAMIC ARRAY OF FLOAT
END RECORD