on 11-21-2018 1:01 AM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can try CL_TREX_JSON_DESERIALIZER like per https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This blog may have the information you want:
https://blogs.sap.com/2013/04/15/abap-2-json-and-json-2-abap-with-st/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.