Home > front end >  Not able to create ABAP structure to use /ui2/cl_json=>deserialize
Not able to create ABAP structure to use /ui2/cl_json=>deserialize

Time:05-22

My json file looks like:

{
    "1489402": {
        "Category": "Program error",
        "CorrectionInstructionsObjectList": [{
            "ObjectName": "/SCMB/CL_IM_ORG_CHECK         IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",
            "ObjectType": "METH",
            "ProgramID": "LIMU"
        }, {
            "ObjectName": "/SCMB/MP556100_F01",
            "ObjectType": "REPS",
            "ProgramID": "LIMU"
        }, {
            "ObjectName": "/SCMB/GET_ORG_STRUCTURE",
            "ObjectType": "FUNC",
            "ProgramID": "LIMU"
        }],
        "CurrentStatus": "Released for Customer",
        "PrimarySAPComponent": "tm-md-org",
        "ReleasedOn": "16.07.2010"
    }
}

I want to create a corresponding ABAP structure in my report so that I can consume this json file and map it into the structure. I want to use /ui2/cl_json=>deserialize but I am not able to fugure out what should be the receiving ABAP type.

 /ui2/cl_json=>deserialize( EXPORTING json = lv_json_content
                               pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                               CHANGING data = lt_data ).

In other words what should be the structure of lt_data.

CodePudding user response:

I don't answer your question because I don't master /ui2/cl_json, but I propose another solution.

As rule of thumb, I wouldn't recommend to use /ui2/cl_json because it's not officially supported by SAP as far as I know (it's just an initiative of one SAP employee), but to use XSLT or SAP Simple Transformation language (preferred). I go for XSLT because ST is impossible to use due to the dynamic property name "1489402" in the JSON file.

  1. Create an XSLT transformation
  2. The ABAP program calls the transformation

Note that when JSON is concerned, SAP converts it into SAP JSON XML format, so the XSLT transformation receives the JSON as SAP JSON XML format (tags like <object>, <array>, <str>), and returns XML in SAP ASXML format so that to initialize an ABAP variable.

XSLT transformation Z_OBJECTS:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:template match="/object/object">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <ROOT>
          <ITEM_NAME>
            <xsl:value-of select="@name"/>
          </ITEM_NAME>
          <CATEGORY>
            <xsl:value-of select="str[@name='Category']"/>
          </CATEGORY>
          <OBJECT_LIST>
            <xsl:for-each select="array/object">
              <item>
                <OBJECT_NAME>
                  <xsl:value-of select="str[@name='ObjectName']"/>
                </OBJECT_NAME>
                <OBJECT_TYPE>
                  <xsl:value-of select="str[@name='ObjectType']"/>
                </OBJECT_TYPE>
                <PROGRAM_ID>
                  <xsl:value-of select="str[@name='ProgramID']"/>
                </PROGRAM_ID>
              </item>
            </xsl:for-each>
          </OBJECT_LIST>
          <CURRENT_STATUS>
            <xsl:value-of select="str[@name='CurrentStatus']"/>
          </CURRENT_STATUS>
          <PRIMARY_SAP_COMPONENT>
            <xsl:value-of select="str[@name='PrimarySAPComponent']"/>
          </PRIMARY_SAP_COMPONENT>
          <RELEASED_ON>
            <xsl:value-of select="str[@name='ReleasedOn']"/>
          </RELEASED_ON>
        </ROOT>
      </asx:values>
    </asx:abap>
  </xsl:template>

</xsl:transform>

ABAP program:

TYPES: BEGIN OF ty_object,
         object_name TYPE string,
         object_type TYPE string,
         program_id  TYPE string,
       END OF ty_object,
       ty_object_list TYPE STANDARD TABLE OF ty_object WITH EMPTY KEY,
       BEGIN OF ty_item,
         item_name             TYPE string, " will contain "1489402"
         category              TYPE string,
         object_list           TYPE ty_object_list,
         current_status        TYPE string,
         primary_sap_component TYPE string,
         released_on           TYPE string,
       END OF ty_item.
DATA(json) = `{    "1489402": {`
    && `        "Category": "Program error",`
    && `        "CorrectionInstructionsObjectList": [{`
    && `            "ObjectName": "/SCMB/CL_IM_ORG_CHECK         IF_EX_HRBAS00_RELAT~MAINTAIN_RELATION",`
    && `            "ObjectType": "METH",`
    && `            "ProgramID": "LIMU"`
    && `        }, {`
    && `            "ObjectName": "/SCMB/MP556100_F01",`
    && `            "ObjectType": "REPS",`
    && `            "ProgramID": "LIMU"`
    && `        }, {`
    && `            "ObjectName": "/SCMB/GET_ORG_STRUCTURE",`
    && `            "ObjectType": "FUNC",`
    && `            "ProgramID": "LIMU"`
    && `        }],`
    && `        "CurrentStatus": "Released for Customer",`
    && `        "PrimarySAPComponent": "tm-md-org",`
    && `        "ReleasedOn": "16.07.2010"}}`.
DATA(item) = VALUE ty_item( ).
CALL TRANSFORMATION z_objects SOURCE XML json RESULT root = item.

For more information, refer to XSLT documentation throughout the Web, and to the SAP JSON XML and ASXML formats in the ABAP documentation.

  • Related