0% found this document useful (0 votes)
21 views4 pages

Join

The document discusses converting a JSON string to an ABAP structure. It shows that using a standard table of strings works for mapping an array of strings in JSON to a table field in ABAP. Using the JSON_TO_DATA ABAP statement is also demonstrated.

Uploaded by

ashfaq.erp3780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Join

The document discusses converting a JSON string to an ABAP structure. It shows that using a standard table of strings works for mapping an array of strings in JSON to a table field in ABAP. Using the JSON_TO_DATA ABAP statement is also demonstrated.

Uploaded by

ashfaq.erp3780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Suppose I have the following JSON data.

{
"data": "abc",
"tab": ["a", "b", "c"]
}<br>

I want to convert this to ABAP structure of the following structure.

|data |tab |
|"abc"|3 lines (first: "a", second: "b, third: "c") |

or,

|data |tab |
|"abc"|"a","b","c" |

At first I thought of defining a type and using /ui2/cl_json=>deserialize as


below.

types: begin of ts_json,


data type string,
tab type string, "this is not right!
end of ts_json.

data(json) = '{ "data": "abc", "tab": ["a", "b", "c"] }'.


data ls_data type ts_json.
/ui2/cl_json=>deserialize(
exporting json = conv #( json )
changing data = ls_data
).

However, I'm stuck because the field "tab" is a table of string which does
not have field names.

If I debug the code, ls_data-data is filled but ls_data-tab is blank.

How can I define such type that can receive an array of string as a table?

it's easy:

types: begin of ts_json,


data type string,
tab type standard table of string with empty key,
end of ts_json.
data ls_data type ts_json.

CALL TRANSFORMATION zst SOURCE XML json RESULT root =


ls_data.

ZST simple transformation (interesting part only):

<object tt:ref=".ROOT">
<str name="data" tt:value-ref="DATA"/>
<array name="tab">
<tt:loop ref="TAB">
<str tt:value-ref="TABLE_LINE"/>
</tt:loop>
</array>
</object>

 SAP Managed Tags:

 The question and its answer proved to be helpful.


 "string_table" type can also be used, so add it.

 METHOD if_oo_adt_classrun~main.

 DATA:
 begin of ls_result,
 data type string,
 * tab type standard table of string with empty key,
 tab TYPE string_table,
 end of ls_result.

 DATA(lv_json_string) = |\{\r\n| &


 | "data": "abc",\r\n| &
 | "tab": ["a", "b", "c"]\r\n| &
 |\}|.

 xco_cp_json=>data->from_string( lv_json_string )->apply( VALUE


#(
 ( xco_cp_json=>transformation->camel_case_to_underscore )
 ( xco_cp_json=>transformation->boolean_to_abap_bool )
 ) )->write_to( REF #( ls_result ) ).

 out->write( ls_result-data ).
 LOOP AT ls_result-tab REFERENCE INTO DATA(lr).
 out->write( lr->* ).
 ENDLOOP.

 ENDMETHOD.

You might also like