cancel
Showing results for 
Search instead for 
Did you mean: 

Transformation for JSON to ABAP

Francis417
Participant
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

alexey_arseniev
Advisor
Advisor
0 Kudos

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.

Francis417
Participant
0 Kudos

Dear Alexey,

Thanks a lot, this is exactly what I needed. Tried with the suggested matching structure and it works perfect.

Regards,

Francis

Answers (3)

Answers (3)

SimoneMilesi
Active Contributor
maheshpalavalli
Active Contributor

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

Francis417
Participant
0 Kudos

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

DoanManhQuynh
Active Contributor
0 Kudos

This blog may have the information you want:

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