<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to convert JSON string array to internal table? in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496644#M2002884</link>
    <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;sandra.rossi&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Thanks for your quick response. Below line is what I required.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;tab  TYPE STANDARD TABLE OF string WITH EMPTY KEY,&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Feb 2022 10:33:01 GMT</pubDate>
    <dc:creator>MioYasutake</dc:creator>
    <dc:date>2022-02-15T10:33:01Z</dc:date>
    <item>
      <title>How to convert JSON string array to internal table?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496642#M2002882</link>
      <description>&lt;P&gt;Hi ABAP experts.&lt;/P&gt;
  &lt;P&gt;Suppose I have the following JSON data.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;{
	"data": "abc",
	"tab": ["a", "b", "c"]
}&amp;lt;br&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I want to convert this to ABAP structure of the following structure.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;|data |tab   |
|"abc"|3 lines (first: "a", second: "b, third: "c") |

or,

|data |tab   |
|"abc"|"a","b","c" |
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;At first I thought of defining a type and using /ui2/cl_json=&amp;gt;deserialize as below.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;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=&amp;gt;deserialize(
  exporting json = conv #( json )
  changing data = ls_data
).
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;However, I'm stuck because the field "tab" is a table of string which does not have field names.&lt;/P&gt;
  &lt;P&gt;If I debug the code, ls_data-data is filled but ls_data-tab is blank.&lt;/P&gt;
  &lt;P&gt;How can I define such type that can receive an array of string as a table?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 08:13:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496642#M2002882</guid>
      <dc:creator>MioYasutake</dc:creator>
      <dc:date>2022-02-15T08:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert JSON string array to internal table?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496643#M2002883</link>
      <description>&lt;P&gt;I don't answer your exact question, but when you know Simple Transformation and JSON-XML, it's easy:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;ZST simple transformation (interesting part only):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;object tt:ref=".ROOT"&amp;gt;
    &amp;lt;str name="data" tt:value-ref="DATA"/&amp;gt;
    &amp;lt;array name="tab"&amp;gt;
        &amp;lt;tt:loop ref="TAB"&amp;gt;
            &amp;lt;str tt:value-ref="TABLE_LINE"/&amp;gt;
        &amp;lt;/tt:loop&amp;gt;
    &amp;lt;/array&amp;gt;
&amp;lt;/object&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 09:35:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496643#M2002883</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2022-02-15T09:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert JSON string array to internal table?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496644#M2002884</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;sandra.rossi&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Thanks for your quick response. Below line is what I required.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;tab  TYPE STANDARD TABLE OF string WITH EMPTY KEY,&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Feb 2022 10:33:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496644#M2002884</guid>
      <dc:creator>MioYasutake</dc:creator>
      <dc:date>2022-02-15T10:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert JSON string array to internal table?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496645#M2002885</link>
      <description>&lt;P&gt;Hi Mio,&lt;/P&gt;&lt;P&gt;Kindly check below code and SNC Community link.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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-&amp;gt;get_entity( )-&amp;gt;get_string_data( ).

    /ui2/cl_json=&amp;gt;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-&amp;gt;create_entity( ).

    lo_entity-&amp;gt;set_content_type( if_rest_media_type=&amp;gt;gc_appl_json ).

    lo_entity-&amp;gt;set_string_data( /ui2/cl_json=&amp;gt;serialize( ls_receive ) ).

    mo_response-&amp;gt;set_status( cl_rest_status_code=&amp;gt;gc_success_ok ).

  ENDMETHOD.
ENDCLASS.&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;■ Debug screen&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2024820-debug.jpg" /&gt;&lt;/P&gt;&lt;P&gt;■ Postman test result&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2024821-postman-test.jpg" /&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer"&gt;■ One more ABAP to JSON Serializer and Deserializer&lt;/A&gt; &lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 01:06:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/12496645#M2002885</guid>
      <dc:creator>former_member718642</dc:creator>
      <dc:date>2022-02-21T01:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert JSON string array to internal table?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/13657226#M2027486</link>
      <description>&lt;P&gt;Mio san,&lt;/P&gt;&lt;P&gt;I faced a similar problem in the ABAP environment of BTP.&lt;/P&gt;&lt;P&gt;The question and its answer proved to be helpful.&lt;/P&gt;&lt;P&gt;"string_table" type can also be used, so add it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;  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| &amp;amp;
                           |    "data": "abc",\r\n| &amp;amp;
                           |    "tab": ["a", "b", "c"]\r\n| &amp;amp;
                           |\}|.

    xco_cp_json=&amp;gt;data-&amp;gt;from_string( lv_json_string )-&amp;gt;apply( VALUE #(
      ( xco_cp_json=&amp;gt;transformation-&amp;gt;camel_case_to_underscore )
      ( xco_cp_json=&amp;gt;transformation-&amp;gt;boolean_to_abap_bool )
    ) )-&amp;gt;write_to( REF #( ls_result ) ).

    out-&amp;gt;write( ls_result-data ).
    LOOP AT ls_result-tab REFERENCE INTO DATA(lr).
      out-&amp;gt;write( lr-&amp;gt;* ).
    ENDLOOP.

  ENDMETHOD.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="horitaku_0-1712142408816.png" style="width: 400px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/90399i04CEBC2CF72385A1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="horitaku_0-1712142408816.png" alt="horitaku_0-1712142408816.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 11:24:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-convert-json-string-array-to-internal-table/m-p/13657226#M2027486</guid>
      <dc:creator>horitaku</dc:creator>
      <dc:date>2024-04-03T11:24:27Z</dc:date>
    </item>
  </channel>
</rss>

