Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JSON to ABAP conversion

0 Likes
2,289

I have a json file as follows-

{
"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"
},
"1490147": {
"Category": "Program error",
"CorrectionInstructionsObjectList": [{
"ObjectName": "/SCMB/CL_ORG_UNIT_MGR",
"ObjectType": "CPRI",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/CL_BPTM_HELPER",
"ObjectType": "CPUB",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/CL_BPTM_HELPER CHECK_ORG_HIERARCHY",
"ObjectType": "METH",
"ProgramID": "LIMU"
}],
"CurrentStatus": "Released for Customer",
"PrimarySAPComponent": "tm-md-bp",
"ReleasedOn": "21.10.2010"
}
}

I need to read this and map into an ABAP internal table.

As far as I know there are 2 ways to do it-

1.

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

In this case what should be the structure of lt_data ?

2. XSLT

Can anyone let me know what should be the XSLT transformation ?

The below one works only for one record:

<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>

Any help would be appreciated.

Thanks.

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,014

Concerning the second solution, with XSLT, a first hint is given here (for almost identical JSON): json - Not able to create ABAP structure to use /ui2/cl_json=>deserialize - Stack Overflow.

By changing the proposed program this way:

DATA items TYPE TABLE OF ty_item.
CALL TRANSFORMATION z_objects SOURCE XML json RESULT root = items.

and the XSLT this way, it will work:

<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">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <ROOT>
          <xsl:for-each select="object">
            <item>
              <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>
            </item>
          </xsl:for-each>
        </ROOT>
      </asx:values>
    </asx:abap>
  </xsl:template>

</xsl:transform>

I hope it will motivate you to read SAP documentation about transformations, XSLT, JSON-XML, asXML, and ask questions about how it works if needed.

I have a json file as follows-

{
"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"
},
"1490147": {
"Category": "Program error",
"CorrectionInstructionsObjectList": [{
"ObjectName": "/SCMB/CL_ORG_UNIT_MGR",
"ObjectType": "CPRI",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/CL_BPTM_HELPER",
"ObjectType": "CPUB",
"ProgramID": "LIMU"
}, {
"ObjectName": "/SCMB/CL_BPTM_HELPER CHECK_ORG_HIERARCHY",
"ObjectType": "METH",
"ProgramID": "LIMU"
}],
"CurrentStatus": "Released for Customer",
"PrimarySAPComponent": "tm-md-bp",
"ReleasedOn": "21.10.2010"
}
}

I need to read this and map into an ABAP internal table.

As far as I know there are 2 ways to do it-

1.

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

In this case what should be the structure of lt_data ?

2. XSLT

Can anyone let me know what should be the XSLT transformation ?

The below one works only for one record:

<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>

Any help would be appreciated.

Thanks.

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
2,014

Please edit your question, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,015

Concerning the second solution, with XSLT, a first hint is given here (for almost identical JSON): json - Not able to create ABAP structure to use /ui2/cl_json=>deserialize - Stack Overflow.

By changing the proposed program this way:

DATA items TYPE TABLE OF ty_item.
CALL TRANSFORMATION z_objects SOURCE XML json RESULT root = items.

and the XSLT this way, it will work:

<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">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <ROOT>
          <xsl:for-each select="object">
            <item>
              <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>
            </item>
          </xsl:for-each>
        </ROOT>
      </asx:values>
    </asx:abap>
  </xsl:template>

</xsl:transform>

I hope it will motivate you to read SAP documentation about transformations, XSLT, JSON-XML, asXML, and ask questions about how it works if needed.

Read only

0 Likes
2,014

Really appreciate your help Sandra 🙂

Read only

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
2,014

Hello siddharth.babbar,

you may try this code, it shall work:

  TYPES:
    BEGIN OF ts_cio,
      object_name TYPE string,
      object_type TYPE string,
      program_id  TYPE string,
    END OF ts_cio,
    BEGIN OF ts_error,
      category              TYPE string,
      ci_list               TYPE STANDARD TABLE OF ts_cio WITH DEFAULT KEY,
      current_status        TYPE string,
      primary_sap_component TYPE string,
      released_on           TYPE d,
    END OF ts_error,
    BEGIN OF ts_dump,
      id    TYPE i,
      error TYPE ts_error,
    END OF ts_dump,
    tt_dump TYPE SORTED TABLE OF ts_dump WITH UNIQUE KEY id.

  DATA: lt_data TYPE tt_dump.

  /ui2/cl_json=>deserialize( EXPORTING json = lv_json
                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                                       assoc_arrays = abap_true
                                       assoc_arrays_opt = abap_true
                                       name_mappings = VALUE #(
                                        ( abap = `CI_LIST` json = `CorrectionInstructionsObjectList` )
                                        ( abap = `PROGRAM_ID` json = `ProgramID` )
                                        ( abap = `PRIMARY_SAP_COMPONENT` json = `PrimarySAPComponent` ) )
                             CHANGING data = lt_data ).