Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert JSON string array to internal table?

MioYasutake
Active Contributor

Hi ABAP experts.

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?

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

I don't answer your exact question, but when you know Simple Transformation and JSON-XML, 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>

4 REPLIES 4

Sandra_Rossi
Active Contributor

I don't answer your exact question, but when you know Simple Transformation and JSON-XML, 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>

Hi sandra.rossi,

Thanks for your quick response. Below line is what I required.

tab  TYPE STANDARD TABLE OF string WITH EMPTY KEY,

0 Kudos

Mio san,

I faced a similar problem in the ABAP environment of BTP.

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.

 

 

Result.

horitaku_0-1712142408816.png

 

0 Kudos

Hi Mio,

Kindly check below code and SNC Community link.

CLASS ycl_rest_imp_test DEFINITION PUBLIC INHERITING FROM cl_rest_resource FINAL CREATE PUBLIC.

  PUBLIC SECTION.
    TYPES: tt_str_tab TYPE STANDARD TABLE OF string WITH EMPTY KEY
           ,
           BEGIN OF ts_data,
             data TYPE string,
             tab  TYPE tt_str_tab,
           END OF ts_data.

    METHODS:
      if_rest_resource~post REDEFINITION.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ycl_rest_imp_test IMPLEMENTATION.
  METHOD if_rest_resource~post.

    DATA: ls_receive  TYPE ts_data,
          lt_response TYPE ts_data.

    DATA(lv_request_body) = mo_request->get_entity( )->get_string_data( ).

    /ui2/cl_json=>deserialize(
      EXPORTING
        json             = lv_request_body
*      jsonx            =
*      pretty_name      =
*      assoc_arrays     =
*      assoc_arrays_opt =
*      name_mappings    =
*      conversion_exits =
      CHANGING
        data             = ls_receive
    ).

    DATA(lo_entity) = mo_response->create_entity( ).

    lo_entity->set_content_type( if_rest_media_type=>gc_appl_json ).

    lo_entity->set_string_data( /ui2/cl_json=>serialize( ls_receive ) ).

    mo_response->set_status( cl_rest_status_code=>gc_success_ok ).

  ENDMETHOD.
ENDCLASS.

■ Debug screen

■ Postman test result

■ One more ABAP to JSON Serializer and Deserializer