Skip to Content
-1

Transformation for JSON to ABAP

Dear Experts,

I am new in dealing with JSON and would like to seek some help from all the experts here. If I have a JSON with the following format, how can I define an ABAP structure so I can use call transformation to transform it to ABAP

JSON :

{ “field1”:{“field2”:value_a,”field3”:value_b,”field4”:{“field5”:value_c,”field6”:{“field7”:value_d,”field8”:value_e}}}}

We do not have any UI addon installed and would like to know if it is possible to transform it using 'Call Transformation'

Thanks,

Francis

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Nov 26, 2018 at 10:02 AM

    Hi Francis,

    the matching structure would be:

    DATA: BEGIN OF ls_data,
            field2 TYPE i,
            field3 TYPE string,
            BEGIN OF field4,
              field5 TYPE string,
              BEGIN OF field6,
                field7 TYPE string,
                field8 TYPE i,
              END OF field6,
            END OF field4,
          END OF ls_data.
    

    And the code to deserialize using simple transformation:

    "lv_json = `{"field1":{"field2":2,"field3":"value_b","field4":{"field5":"value_c","field6":{"field7":"value_d","field8":1}}}}`.
    lv_json = `{"FIELD1":{"FIELD2":2,"FIELD3":"VALUE_B","FIELD4":{"FIELD5":"VALUE_C","FIELD6":{"FIELD7":"VALUE_D","FIELD8":1}}}}`.
    CALL TRANSFORMATION id SOURCE XML lv_json RESULT field1 = ls_data.
    

    But take into account, that your attribute names shall be written in upper case, otherwise, it would not work.

    If this is not possible, you need to first traverse the whole JSON tree and transform all attribute names to upper cases. As it suggested in the standard program DEMO_JSON_NAMES_TO_UPPER (SE38).

    BR, Alexey.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 21, 2018 at 04:20 AM

    Hi Francis S.K. LUK

    You can also convert the same without transformation by using the classes provide by sap. Make sure the abap table is in the format of the json structure.

    https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer

    BR,

    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

    • Dear Mahesh,

      As we don't have any UI addon installed in our environment, I can't find the class /ui2/cl_json. That's why I would like to know if it is possible to use 'Call Transformation' to do the same.

      Regards,

      Francis

  • Nov 21, 2018 at 08:24 AM
    Add comment
    10|10000 characters needed characters exceeded

  • Nov 21, 2018 at 05:19 AM

    This blog may have the information you want:

    https://blogs.sap.com/2013/04/15/abap-2-json-and-json-2-abap-with-st/

    Add comment
    10|10000 characters needed characters exceeded