cancel
Showing results for 
Search instead for 
Did you mean: 

problem to convert Json schema to ABAP internal table

Former Member
0 Kudos

Dear Experts ,

          my database table contains json data , now i have to convert that data into internal table . my tried number of methods but unfortunately i didn't get proper result.

json data is like

{

  "userSession": {

    "name": "X1234500",

    "lastname": "",

    "firstname": "",

    "emailaddress": "",

    "connRegistrationId": "738b584ebf8258c",

    "userSystemId": "xxxxx"

  },

  "bfmData": [

    {

      "controlArea": "1000",

      "rpaGroup": "US"

    },

    {

      "controlArea": "1000",

      "rpaGroup": "TS"

    },

    {

      "controlArea": "1000",

      "rpaGroup": "AS"

    }

  ]

}

how do i convert that data into internal table .

please guide me.

Regards,

Venkatesh.

Accepted Solutions (1)

Accepted Solutions (1)

Sandra_Rossi
Active Contributor
0 Kudos

Please indicate both the expected result, and what you tried.

Former Member
0 Kudos

Thank you for your response sir

      i tried with this classes CL_GIS_JSON_PARSER  and CL_SXML_READER but i am not getting required result .

     i am expecting a result like i need to pass the json data and it should convert into internal table and able to display internal table data  as normal report.

Regards

Sandra_Rossi
Active Contributor
0 Kudos

There is no built-in feature "convert any JSON into internal table".

So, please explain the structure of the target internal table you want, with an example corresponding to your question.

Former Member
0 Kudos

Actually that is my problem , i am in confusion to create a internal table structure for above JSON Data.

how to create a internal table for above json data ??

i think above Json data is requires two internal tables .

In that case , is it possible to import a single JSON data into two internal tables ?

Simply , my problem is to transform above mentioned  Json data into internal table.

your reply will so much helpful to me.

Thank you!

Sandra_Rossi
Active Contributor
0 Kudos

Why do you ask to convert it into an internal table? What do you need to do with the JSON?

Former Member
0 Kudos

i am the trainee in my company with 4 months experience . i just received this task 2 days back. i am not sure about the project details .

Former Member
0 Kudos

i received a custom table which contained two fields , In that one field is full of JSON data as above given.

They said to me that to convert that Json data into internal table.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Venkatesh,

Are you being asked to write the parsing algorithm yourself or simply use the tools within the system to get the job done?

Regards,

Ryan Crosby

Former Member
0 Kudos

i developed a code by including CL_GIS_JSON_PARSER class.

Thank you.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Venkatesh,

If that class is not working I would look for 'CL_TREX_JSON_DESERIALIZER' if you are on a high enough system version.  We don't have it in our ECC system but in our Gateway system it exists and it looks like what you need.

Regards,

Ryan Crosby

Former Member
0 Kudos

Thanks for your valuable suggestion sir,

How to create a internal table structure  for above mentioned JSON data ?

Sandra_Rossi
Active Contributor
0 Kudos

CL_TREX_JSON_DESERIALIZER' is part of package STREXTESTTOOL, thus the note 2141584 - Using Package STREXTESTTOOL applies. It says that if "You want to use objects of package STREXTESTTOOL" and "Unfortunately you are facing issues", then there is no solution as "The package STREXTESTTOOL is intended only for internal SAP Purposes" and that "This package is not supported from SAP side".

Sandra_Rossi
Active Contributor
0 Kudos

Please ask them the structure of the internal table, and the rules how the JSON must fit into it. If you don't do what they expect, then your work will probably have to be done again.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Sandra,

If we limited ourselves to only the things that SAP has released then more than half the system would be off limits.  There are countless technical/functional things that are useful to achieve something and all one has to do is have some patience to analyze how to use it properly.  I would never close the door on using something merely because they would not provide support - if they won't help then I will figure out the problem myself.

Regards,

Ryan Crosby

raj2
Explorer
0 Kudos

Hi,


I think this is useful for u how to create internal table

REPORT demo_st_json_table.

CLASS demo DEFINITION.
   PUBLIC SECTION.
     CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
   METHOD main.
     "Internal table as source of transformation
     DATA(out) = cl_demo_output=>new(
       )->begin_section( 'Internal Table' ).
     DATA: BEGIN OF carrier_wa,
             carrid   TYPE scarr-carrid,
             carrname TYPE scarr-carrname,
             url      TYPE scarr-url,
           END OF carrier_wa,
           carrier_tab LIKE TABLE OF carrier_wa.
     SELECT *
            FROM scarr
            INTO CORRESPONDING FIELDS OF TABLE @carrier_tab.
     out->write_data( carrier_tab ).

     "Simple Transformation to JSON
     out->next_section( 'JSON' ).
     DATA(json_writer) = cl_sxml_string_writer=>create(
                           type = if_sxml=>co_xt_json ).
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE carriers = carrier_tab
                         RESULT XML json_writer.
     DATA(json) = json_writer->get_output( ).
     out->write_json( json ).

     "Simple Transformation to JSON-XML
     out->next_section( 'JSON-XML' ).
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE carriers = carrier_tab
                         RESULT XML DATA(xml).
     out->write_xml( xml ).

     "Assert symmetry of transformation
     DATA result_tab LIKE carrier_tab.
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE XML json
                         RESULT carriers = result_tab.
     ASSERT result_tab = carrier_tab.

     "Assert that results of transformation and JSON-Writer are the same
     DATA(xml_reader) = cl_sxml_string_reader=>create( xml ).
     json_writer = cl_sxml_string_writer=>create(
                     type = if_sxml=>co_xt_json ).
     xml_reader->next_node( ).
     xml_reader->skip_node( json_writer ).
     ASSERT json_writer->get_output( ) = json.

     out->display( ).
   ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
   demo=>main( ).

 

Thanks,

Rajasekhar.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Venkatesh,

You can do something simple like this to get the data - the one catch is that the type you define must match the JSON format.  I have picked a different standard class because of the OSS note mention of no support so you would have to check if you have it in your system:


TYPES: BEGIN OF session,
          name TYPE string,
          lastname TYPE string,
          firstname TYPE string,
          emailaddress TYPE string,
          connregistrationid TYPE string,
          usersystemid TYPE string,
        END OF session,

        BEGIN OF controlarea_s,
          controlarea TYPE string,
          rpagroup TYPE string,
        END OF controlarea_s,

        BEGIN OF root,
          usersession TYPE session,
          bfmdata TYPE TABLE OF controlarea_s WITH NON-UNIQUE DEFAULT KEY,
        END OF root.

DATA: lv_json TYPE string,
       ls_abap TYPE root.

lv_json = '{"userSession":{"name":"X1234500","lastname":"","firstname": "","emailaddress":"","connRegistrationId":"738b584ebf8258c","userSystemId":"xxxxx"},'.
CONCATENATE lv_json '"bfmData":[{"controlArea":"1000","rpaGroup":"US"},{"controlArea":"1000","rpaGroup":"TS"},{"controlArea":"1000","rpaGroup":"AS"}]}' INTO lv_json.
/ui2/cl_json=>deserialize( EXPORTING
                              json = lv_json
                            CHANGING
                              data = ls_abap ).

Regards,

Ryan Crosby

Sandra_Rossi
Active Contributor
0 Kudos

I was just mentioning a fact, that you had not mentioned (and because there is a note about it for this class shows that there is really a problem with it, otherwise SAP wouldn't have created it). It often happens that unsupported objects are removed by SAP.

Answers (1)

Answers (1)

Former Member
0 Kudos

You need to create a structure

1.

s_struct {

userSession type s_user_Session,

bfmData type t_bfmData

}

2.

s_usersession{

name,

lastname,

firstname ,

emailaddress,

connRegistrationId,

userSystemId

}

3. s_bfmData {

controlArea,

rpaGroup

}

You need to write a simple transformation to covert JSON into internal table ....

raj2
Explorer
0 Kudos

Hi,


<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
   <tt:root name="CARRIERS"/>
  <tt:template>
    <array>
       <tt:loop ref=".CARRIERS">
        <object>
           <str name="Carrier-Id">
            <tt:value ref="$ref.carrid"/>
          </str>
           <str name="Carrier">
            <tt:value ref="$ref.carrname"/>
          </str>
           <str name="URL">
            <tt:value ref="$ref.url"/>
           </str>
        </object>
       </tt:loop>
    </array>
  </tt:template>
</tt:transform>

Based on above code created below program.

REPORT demo_st_json_table.

CLASS demo DEFINITION.
   PUBLIC SECTION.
     CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
   METHOD main.
     "Internal table as source of transformation
     DATA(out) = cl_demo_output=>new(
       )->begin_section( 'Internal Table' ).
     DATA: BEGIN OF carrier_wa,
             carrid   TYPE scarr-carrid,
             carrname TYPE scarr-carrname,
             url      TYPE scarr-url,
           END OF carrier_wa,
           carrier_tab LIKE TABLE OF carrier_wa.
     SELECT *
            FROM scarr
            INTO CORRESPONDING FIELDS OF TABLE @carrier_tab.
     out->write_data( carrier_tab ).

     "Simple Transformation to JSON
     out->next_section( 'JSON' ).
     DATA(json_writer) = cl_sxml_string_writer=>create(
                           type = if_sxml=>co_xt_json ).
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE carriers = carrier_tab
                         RESULT XML json_writer.
     DATA(json) = json_writer->get_output( ).
     out->write_json( json ).

     "Simple Transformation to JSON-XML
     out->next_section( 'JSON-XML' ).
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE carriers = carrier_tab
                         RESULT XML DATA(xml).
     out->write_xml( xml ).

     "Assert symmetry of transformation
     DATA result_tab LIKE carrier_tab.
     CALL TRANSFORMATION demo_st_json_table
                         SOURCE XML json
                         RESULT carriers = result_tab.
     ASSERT result_tab = carrier_tab.

     "Assert that results of transformation and JSON-Writer are the same
     DATA(xml_reader) = cl_sxml_string_reader=>create( xml ).
     json_writer = cl_sxml_string_writer=>create(
                     type = if_sxml=>co_xt_json ).
     xml_reader->next_node( ).
     xml_reader->skip_node( json_writer ).
     ASSERT json_writer->get_output( ) = json.

     out->display( ).
   ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
   demo=>main( ).

 

Thanks,

Rajasekhar

Sandra_Rossi
Active Contributor
0 Kudos

Please read the The SCN Rules of Engagement : "Don’t copy / paste other’s content : Make sure your content is original and unique. Don't cross-post (create multiple copies of the same content)"

AND "Share expertise transparently : SCN is a community site that encourages public knowledge sharing. Be open about your knowledge and how you obtained it."

which means: please, paste the link, do not copy the content : ABAP Keyword Documentation - JSON, Simple Transformation for Internal Tables