ABAP JSON Serialization

Generate JSON payload using ABAP data using class /ui2/cl_json

Simple call

Using call to method /ui2/cl_json=>serialize passing the data variable will generate JSON. Data could be structure or a table.

  SELECT SINGLE FROM I_ProductPlantBasic
    FIELDS product, plant, \_product\_text-language , \_product\_text-productname , isbatchmanagementrequired
    WHERE \_product\_text-language = @sy-langu
    INTO @DATA(product) .

  SELECT FROM I_ProductPlantBasic
    FIELDS product, plant, \_product\_text-language , \_product\_text-productname , isbatchmanagementrequired
    WHERE \_product\_text-language = @sy-langu
    INTO TABLE @DATA(products) UP TO 2 ROWS .

  DATA(product_json_payload)  = /ui2/cl_json=>serialize( EXPORTING data = product  ) .
  DATA(products_json_payload) = /ui2/cl_json=>serialize( EXPORTING data = products  ) .


  cl_demo_output=>write_data( product ) .
  cl_demo_output=>write_json( json = product_json_payload ).
  cl_demo_output=>write_data( products ) .
  cl_demo_output=>write_json( json = products_json_payload ).

  cl_demo_output=>display( ) .

Conversion Exit

Conversion Exit can be applied for example on language field by passing parameter conversion_exits = abap_true of method /ui2/cl_json=>serialize

DATA(product_json_payload)  = 
           /ui2/cl_json=>serialize( EXPORTING 
                  data = product 
                  conversion_exits = abap_true 
           ) .

Boolean

Method /ui2/cl_json=>serialize can recognise and handle boolean values as long as they are defined properly.

ABAP ValueJSON String
spacenull
Xtrue
false

Made some adjustments to define field isbatchmanagementrequired as boolean .

  TYPES : BEGIN OF product_type ,
            product                   TYPE I_ProductPlantBasic-product,
            plant                     TYPE I_ProductPlantBasic-plant,
            language                  TYPE i_producttext-language,
            productname               TYPE i_producttext-productname,
            isbatchmanagementrequired TYPE boolean,
          END OF product_type .
  
DATA : product TYPE product_type .

  SELECT SINGLE FROM I_ProductPlantBasic
    FIELDS product, plant, \_product\_text-language , 
           \_product\_text-productname , 
           isbatchmanagementrequired
    WHERE \_product\_text-language = @sy-langu
    INTO @product .

  DATA(product_json_payload)  = /ui2/cl_json=>serialize( 
                                  EXPORTING 
                                     data = product 
                                     conversion_exits = abap_true 
                               ) .

  cl_demo_output=>write_data( product ) .
  cl_demo_output=>write_json( json = product_json_payload ).

  cl_demo_output=>display( ) .

Although the above is technically right as the value from the database was space however SAP treats space as false. A slight mapping would be required to get this right (or false).

  CONSTANTS : BEGIN OF boolean_value ,
                true  TYPE c VALUE 'X',
                false TYPE c VALUE '-',
              END OF boolean_value .

  DATA : product TYPE product_type .

  SELECT SINGLE FROM I_ProductPlantBasic
    FIELDS product, plant, \_product\_text-language , 
           \_product\_text-productname ,
           CASE WHEN isbatchmanagementrequired = @abap_true THEN @boolean_value-true
                ELSE @boolean_value-false
           END  AS isbatchmanagementrequired
    WHERE \_product\_text-language = @sy-langu
    INTO @product .

Labels

You might have noticed the method /ui2/cl_json=>serialize converted the field name to uppercase. This would probably not be desirable in the real-life APIs. Fortunately, there are few (automatic) options available with pretty_name parameter.

lowercase

With pretty_name = /ui2/cl_json=>pretty_mode-low_case all names are converted to lower case.

  DATA(product_json_payload)  =
           /ui2/cl_json=>serialize(
               EXPORTING
                 data = product
                 conversion_exits = abap_true
                 pretty_name = /ui2/cl_json=>pretty_mode-low_case
                 ) .

camelCase

With a small adjustment in the name of abap fields and pretty_name = /ui2/cl_json=>pretty_mode-camel_case an industry standard naming can be achieved. Notice the use of underscore in the field names.

  TYPES : BEGIN OF product_type ,
            product                      TYPE I_ProductPlantBasic-product,
            plant                        TYPE I_ProductPlantBasic-plant,
            language                     TYPE i_producttext-language,
            product_name                 TYPE i_producttext-productname,
            is_batch_management_required TYPE boolean,
          END OF product_type .

  SELECT SINGLE FROM I_ProductPlantBasic
    FIELDS product, plant,
           \_product\_text-language ,
           \_product\_text-productname AS product_name,
           CASE WHEN isbatchmanagementrequired = @abap_true THEN @boolean_value-true
                ELSE @boolean_value-false
           END  AS is_batch_management_required
    WHERE \_product\_text-language = @sy-langu
    INTO @product .

  DATA(product_json_payload)  =
           /ui2/cl_json=>serialize(
               EXPORTING
                 data = product
                 conversion_exits = abap_true
                 pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                 ) .

using name_mappings

Using parameter name_mappings and a mapping table of type /ui2/cl_json=>name_mappings, custom label for each field can be specified.

  DATA name_mappings TYPE /ui2/cl_json=>name_mappings .

  INSERT VALUE /ui2/cl_json=>name_mapping( abap = 'product'                       json = 'material')      INTO TABLE name_mappings .
  INSERT VALUE /ui2/cl_json=>name_mapping( abap = 'plant'                         json = 'Location')      INTO TABLE name_mappings .
  INSERT VALUE /ui2/cl_json=>name_mapping( abap = 'language'                      json = 'lang')          INTO TABLE name_mappings .
  INSERT VALUE /ui2/cl_json=>name_mapping( abap = 'product_name'                  json = 'materialName')  INTO TABLE name_mappings .
  INSERT VALUE /ui2/cl_json=>name_mapping( abap = 'is_batch_management_required'  json = 'BatchManaged')  INTO TABLE name_mappings .


  DATA(product_json_payload)  =
           /ui2/cl_json=>serialize(
               EXPORTING
                 data = product
                 conversion_exits = abap_true
                 name_mappings = name_mappings
                 ) .

Leave a Reply