ENTMF 

The JSON GENERATE Statement

The JSON GENERATE statement converts data to JSON format.

General Format

JSON GENERATE syntax diagram

when-phrase

The when-phrase syntax

generic-suppression-phrase

The generic-suppression-phrase syntax

phrase-1

The phrase-1 syntax

Syntax Rules

  1. Identifier-1 must be either: 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; or an utf-8 group item
  2. When identifier-1 is a national group item, identifier-1 is processed as an elementary data item of category national.
  3. When identifier-1 is an utf-8 group item, identifier-1 is processed as an elementary data item of category utf-8.
  4. When identifier-1 is an alphanumeric group item, identifier-1 is treated as though it were an elementary data item of category alphanumeric.
  5. Identifier-1 must not be defined with the JUSTIFIED clause.
  6. Identifier-1, identifier-2, identifier-5, or identifier-6 cannot be a function identifier.
  7. Identifier-1 can be subscripted or reference modified.
  8. Identifier-1 must not overlap identifier-2 or identifier-3.
  9. Identifier-1 must not be a dynamic-length group item or a dynamic-length elementary item.
  10. 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 (CCSID1200).
    • identifier-1 is of category alphanumeric and the ENCODING phrase is specified with an EBCDIC CCSID, or the 'FROM CODEPAGE' clause. In this case , the current setting of the MFCODESET environment variable is used for the EBCDIC encoding.
  11. 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.
  12. The ENCODING phrase, if specified, determines the encoding of the generated JSON document, and must follow these rules:
    • identifier-7 must be an unsigned integer data item.
    • literal-3 must be an unsigned integer literal.
    • If identifier-1 is alphanumeric, the value of identifier-7 or literal-3 can be either an EBCDIC coded character set identifier (CCSID) or 1208.
    • If identifier-1 is national or UTF-8, the value of identifier-7 or literal-3 must be 1200 or 1208 respectively. FROM CODEPAGE cannot be specified.
  13. If the SUPPRESS phrase is in effect, identifier-1 must still be large enough to contain the generated JSON text before suppression.
  14. Identifier-2 cannot be reference modified, but it can be subscripted.
  15. Identifier-2 must not be a dynamic-length group item or a dynamic-length elementary item.
  16. Identifier-2 must not overlap identifier-1 or identifier-3, or must not specify the RENAMES clause.
  17. If identifier-2 is set to any of the following, they 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; and any group data item whose subordinate data items are all ignored.
  18. 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; and each non-FILLER data-name must be unique within any immediately superordinate group data item.
  19. Identifier-3 must be an integer data item defined without the symbol P in its picture string.
  20. Identifier-3 must not overlap identifier-1, identifier-2, identifier-4, or identifier-5.
  21. Identifier-5 must reference identifier-2 or one of its subordinate data items.
  22. Identifier-4 or identifier-5 cannot be reference modified, subscripted, or a function identifier.
  23. Identifier-4 cannot specify any data item which is ignored by the JSON GENERATE statement.
  24. Literal-1 must be an alphanumeric or national literal containing the name to be generated in the JSON text corresponding to identifier-4. Alternatively, you can specify OMITTED to generate an anonymous JSON object, whose top-level parent name is not generated. When you specify OMITTED, identifier-4 must reference identifier-2.
  25. Identifier-5 must reference a data item that is subordinate to identifier-2 and that is not otherwise ignored by the operation of the JSON GENERATE statement.
  26. Identifier-6 must be a single-byte alphanumeric elementary data item whose data definition entry contains PICTURE X.
  27. Condition-name-1 must be a level-88 item directly subordinate to identifier-6 and can be specified with multiple values or value ranges.
  28. Literal-2 must be a single-byte alphanumeric literal.

General Rules

  1. Identifier-1 is the receiving area for the generated JSON text.
  2. Identifier-2 is the group or elementary data item to be converted to JSON format. If the data name is prefixed with a # character, the character is omitted from the processed result.
  3. If the COUNT phrase is specified, identifier-3 contains (after successful execution of the JSON GENERATE statement) the count of generated JSON character encoding units. If identifier-1 (the receiver) is of category national, the count is in double-bytes. Otherwise, the count is in bytes.
  4. The NAME phrase enables you to override the default JSON names derived from identifier-2 or its subordinate data items.
  5. If the NAME phrase specifies identifier-4 more than once, the last specification is used.
  6. The SUPPRESS phrase enables you to selectively generate output for the JSON GENERATE statement by allowing you to identify and conditionally exclude items that are subordinate to identifier-2.
  7. When the SUPPRESS phrase is specified, a group item subordinate to identifier-2 is excluded from the generated JSON text if all eligible items subordinate to the group item are excluded. The outermost object that corresponds to identifier-2 itself is always generated, even if all subordinate items to identifier-2 are excluded. In this case, the value generated for identifier-2 is an empty object, as follows :
    {“identifier-2”:{}}

    Unless explicitly suppressed, an ODO table with no elements is always retained in the JSON text as follows:

    {“table-name”:[]}
  8. Identifier-5 explicitly identifies items for potential suppression. It must reference a data item subordinate to identifier-2 that is not otherwise ignored by the JSON GENERATE operation.
  9. If the WHEN phrase is specified, identifier-5 must reference an elementary data item, but if the WHEN phrase is omitted, identifier-5 can be a group item. When a group data item is identified, that group data item and all subordinate data items are excluded.
  10. Duplicate specifications of identifier-5 are permitted.
  11. The rules for potential suppression when using identifier-5 are as follows:
    • When specifying WHEN ZERO/ZEROES/ZEROS, identifier-5 cannot be of USAGE DISPLAY-1.
    • When specifying WHEN SPACE/SPACES, identifier-5 must be of USAGE DISPLAY, DISPLAY-1, or NATIONAL. If it is a zoned or national decimal item, it must be an integer.
    • When specifying WHEN LOW-VALUE/LOW-VALUES/HIGH-VALUE/HIGH-VALUES, identifier-5 must be of USAGE DISPLAY or NATIONAL. If it is a zoned or national decimal item, it must be an integer.
  12. The generic-suppression-phrase identifies elementary items subordinate to identifier-2, that are not otherwise ignored by JSON GENERATE operations, for potential suppression. These are identified according to the NUMERIC and NONUMERIC keywords; omit both keywords to identify both types.
  13. The effect of specifying multiple generic-suppression-phrases is cumulative.
  14. The rules for potential suppression when using the generic-suppression-phrase are as follows:
    • If ZERO, ZEROES, or ZEROS is specified in the WHEN phrase, all data items except those that are defined with USAGE DISPLAY-1 are selected.
    • If SPACE or SPACES is specified in the WHEN phrase, data items of USAGE DISPLAY, DISPLAY-1, or NATIONAL are selected. For zoned or national decimal items, only integers are selected.
    • If LOW-VALUE, LOW-VALUES, HIGH-VALUE, or HIGH-VALUES is specified in the WHEN phrase, data items of USAGE DISPLAY or NATIONAL are selected. For zoned or national decimal items, only integers are selected.
  15. When determining potential suppression items, if the item is of class numeric and WHEN ZERO/ZEROES/ZEROS has been specified, a numeric comparison is performed. For all other items, the comparison is alphanumeric, DBCS, or national, depending on whether the item is of usage DISPLAY, DISPLAY-1, or NATIONAL, respectively.
  16. The CONVERTING phrase enables you to specify items that will be generated as JSON BOOLEAN name/value pairs. The condition-name-1 and literal-2 specifications represent the generated JSON BOOLEAN true value, whereas all other values are generated as JSON BOOLEAN false. The ALSO keyword can be used to specify multiple JSON BOOLEAN name/value pairs.
  17. If the ON EXCEPTION phrase is specified, control transfers to imperative-statement-1 when an error occurs during generation of the JSON document.
  18. 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 in the event of an error.
  19. In the event of an error, the JSON-CODE special register contains the exception code; see JSON-CODE Exception Codes for more information.
  20. If an exception condition does not occur during generation of the JSON document, control is passed to imperative-statement-2, if specified; otherwise, control is passed to the end of the JSON GENERATE statement. The ON EXCEPTION phrase, if specified, is ignored, and special register JSON-CODE contains zero after execution of the JSON GENERATE statement.
  21. The END-JSON phrase is the scope terminator that delimits the scope of the statement.
  22. Use of END-JSON allows a conditional JSON GENERATE statement (that is, a JSON GENERATE statement that specifies the ON EXCEPTION or NOT ON EXCEPTION phrase) to be nested in another conditional statement.
  23. The scope of a conditional JSON GENERATE statement can be terminated by either an END-JSON phrase or a separator period.
  24. END-JSON can also be used with a JSON GENERATE statement that does not specify either the ON EXCEPTION or the NOT ON EXCEPTION phrase.
  25. JSON GENERATE statements that appear in imperative-statement-1 or imperative-statement-2 of another JSON GENERATE statement are nested JSON GENERATE statements. Nested JSON GENERATE statements are considered to be matched JSON GENERATE and END-JSON combinations, proceeding from left to right. Thus, any END-JSON phrase that is encountered is matched with the nearest preceding JSON GENERATE statement that has not been implicitly or explicitly terminated.
  26. If the data name for identifier-2 is prefixed with a # character, the character is omitted from the processed result.