<?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: Dynamically create internal table from JSON using /UI2/CL_JSON and more in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147348#M1976651</link>
    <description>&lt;SPAN class="mention-scrubbed"&gt;rllabresp&lt;/SPAN&gt; Please open a new question if you want help for RTTS/read &amp;lt;FSDATA&amp;gt; (currently, I don't even understand what you're trying to achieve, because reading &amp;lt;FSDATA&amp;gt; doesn't require CL_ABAP_REFDESCR at all).</description>
    <pubDate>Tue, 07 Apr 2020 19:13:30 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2020-04-07T19:13:30Z</dc:date>
    <item>
      <title>Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147345#M1976648</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;
  &lt;P&gt;I have the need to call an API which returns a JSON structure which may change (add or remove fields) although the fields I need will always be there -not sure the position- but there.&lt;/P&gt;
  &lt;P&gt;I've just discovered class /UI2/CL_JSON and it works great to get the JSON and prettify it, but then I've not been able to create an internal table dinamically to then access the data.&lt;/P&gt;
  &lt;P&gt;I've tried all I know about RTTS and the cl_abap_xxdescr classes without success. I can move the data to a field-symbol and see it, but then I cannot do anything else that going around the CL_ABAP_REFDESCR class, and so I cannot read the structure.&lt;/P&gt;
  &lt;P&gt;Anyone can help me?&lt;/P&gt;
  &lt;P&gt;Thanks!!!!!&lt;/P&gt;
  &lt;P&gt;This is the JSON structure&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;{
  "d": {
    "results": [
      {
        "RebateNo": "1234567890",
        "ValidFromDate": "/Date(1427839200000)/",
        "ValidToDate": "/Date(1459375200000)/",
        "Type": "ZZZZ",
        "Status": "Z",
        "SalesGroup": "Z",
        "Owner": "JOHN SMITH",
        "CustomerNo": "12345",
        "CustomerName": "JOHN SMITH",
        "CustomerVAT": "12345"
        }
      },
	.....
}
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;This is the code I have so far. &lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;
TRY.
    CALL TRANSFORMATION sjson2html SOURCE XML response
                                   RESULT XML DATA(formatted_json).

  CATCH cx_xslt_runtime_error.
    cl_abap_browser=&amp;gt;show_html(
      EXPORTING
        title       = 'Something went wrong - &amp;lt;ESC&amp;gt; to continue'
        html_string = response ).
ENDTRY.

IF formatted_json IS NOT INITIAL.

  DATA: temp TYPE REF TO data.
  FIELD-SYMBOLS: &amp;lt;fsdata&amp;gt; type data.

  CALL METHOD /ui2/cl_json=&amp;gt;generate
    EXPORTING
      json        = response
      pretty_name = /ui2/cl_json=&amp;gt;pretty_mode-camel_case
*     name_mappings =
    RECEIVING
      rr_data     = temp.

  DATA(lo_data) = /ui2/cl_data_access=&amp;gt;create( ir_data = temp iv_component = 'd-results' )-&amp;gt;ref( ).

  IF lo_data IS BOUND.
    ASSIGN lo_data-&amp;gt;* TO &amp;lt;fsdata&amp;gt;.

    data lo_desc TYPE REF TO CL_ABAP_TYPEDESCR.

    lo_desc =  cl_abap_refdescr=&amp;gt;describe_by_data( p_data =  lo_data ).

  endif.

    cl_abap_browser=&amp;gt;show_html(
      EXPORTING
        title       = 'JSON File - &amp;lt;ESC&amp;gt; to continue'
        html_string = cl_abap_codepage=&amp;gt;convert_from( formatted_json ) ).

ENDIF.

&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;This is what I see in the field symbol: table and deep structure, but without name so I cannot access it.&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1792933-debug-ui2-json.jpg" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Apr 2020 18:27:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147345#M1976648</guid>
      <dc:creator>rllabresp</dc:creator>
      <dc:date>2020-04-06T18:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147346#M1976649</link>
      <description>&lt;P&gt;You don't need to define the internal table dynamically.   If you can be sure that the fields you need are in the structure, just pre-define the fields that you are interested in.   The JSON parser will ignore fields that are not relevant:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;REPORT ytest_json.

"Define the structure with the fields you are interested in:
TYPES:
  BEGIN OF ty_result,
    RebateNo TYPE string,
    Type     TYPE string,
  END OF ty_result,

  BEGIN OF ty_data,
    BEGIN OF d,
      results TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY,
    END OF d,
  END OF ty_data.

  DATA: lt_data TYPE ty_data.

END-OF-SELECTION.
"Messy hard-coded JSON string with some missing fields due to the 255 char limit
    DATA(lv_json_string) = `{"d":{"results":[{"RebateNo":"1234567890","Type":"ZZZZ","Status":"Z","SalesGroup":"Z","Owner":"JOHN SMITH","CustomerNo":"12345","CustomerName":"JOHN SMITH","CustomerVAT":"12345"}]}}`.

"...and deserialize your JSON
   /ui2/cl_json=&amp;gt;deserialize(
     EXPORTING
        json             = lv_json_string
*       jsonx            =
        pretty_name      = /ui2/cl_json=&amp;gt;pretty_mode-camel_case
*       assoc_arrays     =
*       assoc_arrays_opt =
*       name_mappings    =
*       conversion_exits =
     CHANGING
       data             = lt_data
   ).
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Apr 2020 02:49:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147346#M1976649</guid>
      <dc:creator>EstiV</dc:creator>
      <dc:date>2020-04-07T02:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147347#M1976650</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;thanks, this worked for me!&lt;/P&gt;&lt;P&gt;However, I'm still curious about doing this dynamically. I'll keep investigating, but any idea is more than welcome.&lt;/P&gt;&lt;P&gt;Thank you so much,  &lt;SPAN class="mention-scrubbed"&gt;esti&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 06:28:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147347#M1976650</guid>
      <dc:creator>rllabresp</dc:creator>
      <dc:date>2020-04-07T06:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147348#M1976651</link>
      <description>&lt;SPAN class="mention-scrubbed"&gt;rllabresp&lt;/SPAN&gt; Please open a new question if you want help for RTTS/read &amp;lt;FSDATA&amp;gt; (currently, I don't even understand what you're trying to achieve, because reading &amp;lt;FSDATA&amp;gt; doesn't require CL_ABAP_REFDESCR at all).</description>
      <pubDate>Tue, 07 Apr 2020 19:13:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147348#M1976651</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2020-04-07T19:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147349#M1976652</link>
      <description>&lt;P&gt;Hi Rafael,&lt;/P&gt;&lt;P&gt;I came across your question as I had a very similar issue. I moved the code into a report shape so you can run it easily, The way I solved it dynamically is like that:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;REPORT zjson2abap.

DATA:
  lv_json_body     TYPE string,
  lmsg_data        TYPE REF TO data,
  lo_pos_struct    TYPE REF TO cl_abap_structdescr.

FIELD-SYMBOLS:
  &amp;lt;l_msg_data&amp;gt;   TYPE any,
  &amp;lt;l_postab_ref&amp;gt; TYPE any,
  &amp;lt;lt_postab&amp;gt;    TYPE ANY TABLE,
  &amp;lt;l_pos&amp;gt;        TYPE any,
  &amp;lt;l_fvalue&amp;gt;     TYPE any.


lv_json_body = '{  "tenantId": "949043908", "capabilityId": "da21a3ad-6050-47b5-b1be-daf721c8748b", "sensorId": "feef4fff-0731-462e-9972-ee92923de3fd", "timestamp": 1600244897484, "gatewayTimestamp": 1600244897484,  ' &amp;amp;&amp;amp;
               '"measures": [  {  "tagId": "A32F", "zC": 1000.132, "yC": 1000.132, "xC": 324.12, "quality": 92 } ] }'.

" Generate dynamic structure to suit the data
lmsg_data = /ui2/cl_json=&amp;gt;generate( json = lv_json_body ).
ASSIGN lmsg_data-&amp;gt;* TO &amp;lt;l_msg_data&amp;gt;.

" Deserialize the JSON string into the matching deep datatype
/ui2/cl_json=&amp;gt;deserialize( EXPORTING json = lv_json_body pretty_name = /ui2/cl_json=&amp;gt;pretty_mode-camel_case CHANGING data = &amp;lt;l_msg_data&amp;gt; ).

" Save the position data in resource table
ASSIGN COMPONENT 'MEASURES' OF STRUCTURE &amp;lt;l_msg_data&amp;gt; TO &amp;lt;l_postab_ref&amp;gt;.
ASSIGN &amp;lt;l_postab_ref&amp;gt;-&amp;gt;* TO &amp;lt;lt_postab&amp;gt;.

" Loop over the table
LOOP AT &amp;lt;lt_postab&amp;gt; ASSIGNING FIELD-SYMBOL(&amp;lt;l_posref&amp;gt;).
  ASSIGN &amp;lt;l_posref&amp;gt;-&amp;gt;* TO &amp;lt;l_pos&amp;gt;.
  lo_pos_struct ?= cl_abap_typedescr=&amp;gt;describe_by_data( &amp;lt;l_pos&amp;gt; ).
  DATA(lt_pos_comp) = lo_pos_struct-&amp;gt;get_components( ).
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE &amp;lt;l_pos&amp;gt; TO FIELD-SYMBOL(&amp;lt;l_field_ref&amp;gt;).
    IF sy-subrc &amp;lt;&amp;gt; 0. EXIT. ENDIF.
    ASSIGN &amp;lt;l_field_ref&amp;gt;-&amp;gt;* TO &amp;lt;l_fvalue&amp;gt;.
    WRITE: / |Field: { lt_pos_comp[ sy-index ]-name } Value: { &amp;lt;l_fvalue&amp;gt; }|.
  ENDDO.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Report output should look like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Field: QUALITY Value: 92
Field: TAG_ID Value: A32F
Field: X_C Value: 324.12
Field: Y_C Value: 1000.1319999999999
Field: Z_C Value: 1000.1319999999999
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I believe it won't win a beauty contest but it runs completely dynamic. Trick is that every level in the generated structure has a reference level in between and needs dereferencing. Hope it helps!&lt;/P&gt;&lt;P&gt;Gunter&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 15:30:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147349#M1976652</guid>
      <dc:creator>Gunter</dc:creator>
      <dc:date>2020-09-16T15:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create internal table from JSON using /UI2/CL_JSON and more</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147350#M1976653</link>
      <description>&lt;P&gt;Thanks for sharing &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 08:00:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamically-create-internal-table-from-json-using-ui2-cl-json-and-more/m-p/12147350#M1976653</guid>
      <dc:creator>dirk_stbing</dc:creator>
      <dc:date>2022-05-11T08:00:03Z</dc:date>
    </item>
  </channel>
</rss>

