JSON GENERATE statement
The JSON GENERATE statement converts data to JSON format.
You can watch this video to get an overview of the JSON support in Enterprise COBOL 6.
- identifier-1
- The receiving area for the generated JSON text. identifier-1 must reference
one of the following items:
- An elementary data item of category alphanumeric
- An alphanumeric group item
- An elementary data item of category national
- A national group item
An elementary data item of category UTF-8
A UTF-8 group item
When identifier-1 references an alphanumeric group item, identifier-1 is treated as though it were an elementary data item of category alphanumeric. When identifier-1 references a national group item, identifier-1 is processed as an elementary data item of category national.
When identifier-1 references a UTF-8 group item, identifier-1 is processed as an elementary data item of category UTF-8.
identifier-1 must not be defined with the JUSTIFIED clause, and cannot be a function identifier. identifier-1 can be subscripted or reference modified.
identifier-1 must not overlap identifier-2 or identifier-3.
identifier-1 can be a dynamic-length elementary item of category alphanumeric or UTF-8. identifier-1 cannot be a dynamic-length group item of any category, nor an elementary item of category national.
The generated JSON text is encoded in UTF-8 (CCSID 1208), except in the following scenarios:
- identifier-1 is of category national, in which case it is encoded in UTF-16 Big-Endian (CCSID1200)
- identifier-1 is of category alphanumeric and the ENCODING phrase is specified with an EBCDIC CCSID
Conversion of the data values and NAME phrase literals is done according to the CODEPAGE compiler option in effect for the compilation. Conversion of original data names is always done using CCSID 1140. For details, see CODEPAGE in the Enterprise COBOL Programming Guide.
identifier-1 must be large enough to contain the generated JSON text. Typically, it should be from 2 to 3 times the size of identifier-2, depending on the lengths of the data-names within identifier-2. If identifier-1 is not large enough, an exception condition exists at the end of the JSON GENERATE statement. If the COUNT phrase is specified, identifier-3 contains the number of character encoding units that were actually generated.
- identifier-2
- The group or elementary data item to be converted to JSON format.
identifier-2 must reference one of the following items:
- An elementary data item of category alphanumeric
- An alphanumeric group item
- An elementary data item of category national
- A national group item
- An elementary data item of category UTF-8
- A UTF-8 group item
identifier-2 cannot be a function identifier or be reference modified, but it can be subscripted.
identifier-2 must not overlap identifier-1 or identifier-3.
identifier-2 can be a dynamic-length group item or a dynamic-length elementary item of category alphanumeric or UTF-8. identifier-2 cannot be a dynamic-length group or elementary item of category national.
The data description entry for identifier-2 must not contain a RENAMES clause.
The following data items that are specified by identifier-2 are ignored by the JSON GENERATE statement:
- Any subordinate unnamed elementary data items or elementary FILLER data items
- Any slack bytes inserted for SYNCHRONIZED items
- Any data item subordinate to identifier-2 that is defined with the REDEFINES clause or that is subordinate to such a redefining item
- Any data item subordinate to identifier-2 that is defined with the RENAMES clause
- Any group data item whose subordinate data items are all ignored
All data items specified by identifier-2 that are not ignored according to the previous rules must satisfy the following conditions:
- Each elementary data item must have a USAGE other than POINTER, FUNCTION-POINTER, PROCEDURE-POINTER, or OBJECT REFERENCE.
- There must be at least one such elementary data item.
- Each non-FILLER data-name must have a unique identifier within identifier-2.
Example 1
For example, consider the following data declaration:
01 STRUCT. 02 STAT PIC X(4). 02 IN-AREA PIC X(100). 02 OK-AREA REDEFINES IN-AREA. 03 FLAGS PIC X. 03 PIC X(3). 03 COUNTER USAGE COMP-5 PIC S9(9). 03 ASFNPTR REDEFINES COUNTER USAGE FUNCTION-POINTER. 03 UNREFERENCED PIC X(92). 02 NG-AREA1 REDEFINES IN-AREA. 03 FLAGS PIC X. 03 PIC X(3). 03 PTR USAGE POINTER. 03 ASNUM REDEFINES PTR USAGE COMP-5 PIC S9(9). 03 PIC X(92). 02 NG-AREA2 REDEFINES IN-AREA. 03 FN-CODE PIC X. 03 UNREFERENCED PIC X(3). 03 QTYONHAND USAGE BINARY PIC 9(5). 03 DESC USAGE NATIONAL PIC N(40). 03 UNREFERENCED PIC X(12).
The following data items from the example can be specified as identifier-2:
STRUCT
, of which subordinate data itemsSTAT
andIN-AREA
would be converted to JSON format. (OK-AREA
,NG-AREA1
, andNG-AREA2
are ignored because they specify the REDEFINES clause.)OK-AREA
, of which subordinate data itemsFLAGS
,COUNTER
, andUNREFERENCED
would be converted. (The item whose data description entry specifies03 PIC X(3)
is ignored because it is an elementary FILLER data item.ASFNPTR
is ignored because it specifies the REDEFINES clause.)- Any of the elementary data items that are subordinate to
STRUCT
except:ASFNPTR
orPTR
(disallowed usage)UNREFERENCED OF NG-AREA2
(nonunique names for data items that are otherwise eligible)- Any FILLER data items
The following data items cannot be specified as identifier-2:
NG-AREA1
, because subordinate data itemPTR
specifies USAGE POINTER but does not specify the REDEFINES clause. (PTR
would be ignored if it is defined with the REDEFINES clause.)NG-AREA2
, because subordinate elementary data items have the nonunique nameUNREFERENCED
.
- COUNT phrase
-
If the COUNT phrase is specified,
identifier-3 contains (after successful execution of the JSON GENERATE statement)
the count of
generated JSON character code points
. If identifier-1 (the receiver) is of category national, the count is in double-bytes.
If identifier-1 is of category UTF-8, the count is in
Unicode code points
.
Otherwise, the count is in bytes.
- identifier-3
- The data count field. Must be an integer data item defined without the symbol P in its picture
string.
identifier-3 must not overlap identifier-1, identifier-2, identifier-4, or identifier-5.
INDICATING phrase
The INDICATING phrase is specified with an indicated item, identifier-9, and an indicator item. The indicator item is specified either directly via identifier-10, or indirectly as the parent elementary item of condition-name-2.
ENCODING phrase
The ENCODING phrase, if specified, determines the encoding of the generated JSON document. The ENCODING phrase must follow these rules:
- identifier-6 must be an unsigned integer data item.
- literal-3 must be an unsigned integer literal.
- If identifier-1 is alphanumeric:
- The value of identifier-6 or literal-3 can be either an EBCDIC coded character set identifier (CCSID) from the below Table 1. "Single byte EBCDIC coded character sets for JSON documents" or 1208.
- If FROM CODEPAGE was specified, the CCSID of the CODEPAGE compiler option is assumed and must be an EBCDIC CCSID from the below Table 1. "Single byte EBCDIC coded character sets for JSON documents".
- If identifier-1 is national or UTF-8:
- The value of identifier-6 or literal-3 must be 1200 or 1208 respectively.
- FROM CODEPAGE cannot be specified.
If the ENCODING phrase is omitted and:- If identifier-1 is alphanumeric or UTF-8, the JSON document is encoded in Unicode UTF-8 (CCSID 1208).
- If identifier-1 is national, the JSON document is encoded in Unicode UTF-16 (CCSID 1200).
Table 1. Single byte EBCDIC coded character sets for JSON documents CCSID Description 1047 Latin 1/Open Systems 1140, 37 USA, Canada,…Euro Country Extended Code Page (ECECP), Country Extended Code Page (CECP) 1141, 273 Austria, Germany ECECP, CECP 1142, 277 Denmark, Norway ECECP, CECP 1143, 278 Finland, Sweden ECECP, CECP 1144, 280 Italy ECECP, CECP 1145, 284 Spain, Latin America (Spanish) ECECP, CECP 1146, 285 UK ECECP, CECP 1147, 297 France ECECP, CECP 1148, 500 International ECECP, CECP 1149, 871 Iceland ECECP, CECP If the ENCODING phrase specifies a single byte EBCDIC CCSID, literal-1 of the JSON GENERATE NAME phrase must be an alphanumeric literal.
- NAME phrase
- Allows you to override the default JSON names derived from identifier-2 or its subordinate data items.
- SUPPRESS phrase
- Allows you to identify and exclude items that are subordinate to identifier-2, and thus selectively generate output for the JSON GENERATE statement. If the SUPPRESS phrase is specified, identifier-1 must be large enough to contain the generated JSON document before any suppression.
CONVERTING phrase
Allows you to specify items that will be generated as either JSON BOOLEAN or JSON null name/value pairs.
- converting-phrase Format 1
- Use Format 1 to specify items that will be generated as JSON boolean name/value
pairs.
identifier-7 must be a single-byte alphanumeric elementary data item whose data definition entry contains PICTURE X.
condition-name-1 and literal-2 represent values of identifier-7 that will be generated as a JSON BOOLEAN true value. All other values of identifier-7 will be generated as a JSON BOOLEAN false value. condition-name-1 must be a level-88 item directly subordinate to identifier-7 and can be specified with multiple values or value ranges. literal-2 must be a single-byte alphanumeric literal.
The CONVERTING phrase can be specified with multiple items to be generated as JSON BOOLEAN name/value pairs by using the ALSO keyword.
For example, consider the following COBOL structure and statements:01 myrecord. 02 data-a PIC X. 02 data-b PIC X. 88 data-b-flag VALUE ‘a’ THRU ‘z’. MOVE ‘F’ TO data-a MOVE ‘b’ TO data-b
The output of the JSON GENERATE statement would be the UTF-8 encoded text in docx containing the following JSON text:JSON GENERATE docx FROM myrecord CONVERTING data-a TO BOOLEAN USING ‘T’ ALSO data-b TO BOOLEAN USING data-b-flag
{ “myrecord” : { “data-a” : false, “data-b” : true } }
converting-phrase Format 2
Use Format 2 to specify items that might be generated as JSON null name/value pairs.
identifier-8 must be a group or elementary item that references identifier-2 or is subordinate to identifier-2.
fig-con-1 represents one of the figurative constants from the list below:- SPACE, SPACES
- ZERO, ZEROES, ZEROS
- LOW-VALUE, LOW-VALUES
- HIGH-VALUE, HIGH-VALUES
During execution of the JSON GENERATE statement, fig-con-1 will be compared to identifier-8 in an IS EQUAL relation condition. If the result of the comparison is true, the value of identifier-8 will be JSON null. Otherwise the value of identifier-8 will be generated as per the general rules of JSON GENERATE. fig-con-1 must be a permissible pair for comparison according to Table 3. "Comparisons involving figurative constants".
The following example shows a COBOL structure and statements generating JSON null values using the CONVERTING phrase:01 docx pic x(100). 01 my-record. 02 data-a pic 9999. 02 data-b pic x(10). MOVE zero TO data-a MOVE low-values TO data-b JSON GENERATE docx FROM my-record CONVERTING data-a TO NULL USING zero ALSO data-b TO NULL USING low-values END-JSON
The output of the JSON GENERATE statement would be the UTF-8 encoded text in docx containing the following JSON text:{ "myrecord" : { "data-a" : null, "data-b" : null } }
Note: identifier-7 of Format 1 and identifier-8 might reference the same data item, which might be useful when you want to convert the item to JSON true, false, or null.
- ON EXCEPTION phrase
- An
exception condition exists when an error occurs during generation
of the JSON document, for example if identifier-1 is
not large enough to contain the generated JSON document. In this case,
JSON generation stops and the content of the receiver, identifier-1,
is undefined. If the COUNT phrase is specified, identifier-3 contains
the number of character positions that were actually generated.
If the ON EXCEPTION phrase is specified, control is transferred to imperative-statement-1. If the ON EXCEPTION phrase is not specified, the NOT ON EXCEPTION phrase, if any, is ignored, and control is transferred to the end of the JSON GENERATE statement. Special register JSON-CODE contains an exception code, as detailed in JSON GENERATE exceptions in the Enterprise COBOL Programming Guide.
- NOT ON EXCEPTION phrase
- If an exception condition does not occur during generation of the JSON document, control is passed to imperative-statement-2, if specified, otherwise to the end of the JSON GENERATE statement. The ON EXCEPTION phrase, if specified, is ignored. Special register JSON-CODE contains zero after execution of the JSON GENERATE statement.
- END-JSON phrase
-
This explicit scope terminator delimits the scope of the JSON GENERATE or
JSON PARSE statements. END-JSON permits
a conditional JSON GENERATE or JSON PARSE statement
(that is, a JSON GENERATE or JSON PARSE statement
that specifies the ON EXCEPTION or NOT ON EXCEPTION phrase) to be
nested in another conditional statement.
The scope of a conditional JSON GENERATE or JSON PARSE statement can be terminated by:
- An END-JSON phrase at the same level of nesting
- A separator period
END-JSON can also be used with a JSON GENERATE or JSON PARSE statement that does not specify either the ON EXCEPTION or the NOT ON EXCEPTION phrase.
For more information about explicit scope terminators, see Delimited scope statements.