cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve data from a fieldymbol nested of deep structures

Georg-Stefan
Explorer
0 Kudos
313

Hi *.

I am just working on nested dynamic structures as output of a JSON deserializsation.

At the moment I don't know how to retrieve the data from this nested dynamic structure as I don't see how to get down to the lowest level of the structure. Perhaps you have an idea?

Is start with lo_json_data as output of a deserialization method defined as type ref to data.

 

lo_json_data type ref to data.

field-symbols: <data>    type any,
               <objects> type any.

assign lo_json_data->* to <data>.

 

I can assign the reference of lo_json_data to a field-symbol <data> of type any to just have a look on the data structure.

As I know the structure of the JSON-File

 

{
  "objects":[
    {
      "object":{
         "properties":{
            "CbaseTypeId":{
            "id":"CbaseTypeId",
            "type":"id",
            "cardinality":"single",
            "value":"Cdocument"
            },
            "%CobjectTypeId":{
            "id":"%CobjectTypeId!,

            ...
            
            "%CcontentStreamDataLength":{
            "id":"%CcontentStreamDataLength",
            "type":"integer",
            "cardinality":"single",
            "value":47336
            }
          }
     },
     "pathSegment":"data"
     }
   ],
   "hasMoreItems":false
}

 

I can also define the (hopefully) corresponding ABAP based structure to be read bottom up:

 

types:
  begin of ty_table_line,
    tline type string,
  end of ty_table_line.

types:
  begin of ty_basetype,
    id          type string,
    type        type string,
    cardinality type string,
    value       type standard table of table_line with empty key,
  end of ty_basetype .

types:
  begin of ty_property,
    propertyname type string,
    property     type standard table of ty_basetype with empty key,
  end of ty_property .

types:
  begin of ty_object,
    properties type standard table of ty_property with empty key,
  end of ty_object .

types:
  begin of ty_objects,
    object      type standard table of ty_object with empty key,
    pathsegment type string,
  end of ty_objects .

types:
  begin of ty_table,
    objects         type standard table of ty_objects with empty key,
    hasmoreitems(1),
  end of ty_table .

 

 From the deserialization-Method I get lo_json_data as a deep structure.

The debugger shows me:

GeorgStefan_0-1728910537355.png

I  assign it to <data>. with  assign lo_json_data->to <data>.

GeorgStefan_0-1728911155742.png

To show the structure of <data> I see the first level of the JSON-structure "OBJECTS"

GeorgStefan_1-1728911222558.png

When clicking on "OBJECTS" I get the second deep structure level "OBJECT":

GeorgStefan_2-1728911319511.png

The deep structure OBJECT again contains the deep structure PROPERTIES:

GeorgStefan_3-1728911388453.png

And the table PROPERTIES contains a undefined number of "basetypes" 

<DATA>-OBJECTS[1]-OBJECT-PROPERTIES

GeorgStefan_4-1728911447589.png

Every basetype is structured  with an ID, TYPE, CARDINALITY and a VALUE-Table, 

like the basetype %CSECONDARYOBJECTTYPEIDS as example.

<DATA>-OBJECTS[1]-OBJECT-PROPERTIES-%CSECONDARYOBJECTTYPEIDS

GeorgStefan_5-1728911548546.png

The VALUE can contain sometimes two ore more values:

<DATA>-OBJECTS[1]-OBJECT-PROPERTIES-%CSECONDARYOBJECTTYPEIDS-VALUE

GeorgStefan_6-1728911626935.png

I get stuck iterating to the bottom of the deep structures even though I know the structure is always the same.

<DATA>-OBJECTS[1]-OBJECT-PROPERTIES

Any hints or ideas?

View Entire Topic
Georg-Stefan
Explorer

The solution looks a bit different, but anyhow your "LOOPS" gave me the idea:

 

 

data:  ls_value_tab   type ty_table_line,
          lt_value_tab   type table of ty_table_line,
          ls_response    type ty_basetype,
          lt_response    type standard table of ty_basetype,
          lv_count       type i.

    field-symbols: <data>        type any,
                   <data2>       type any table,
                   <objects>     type any,
                   <object>      type any,
                   <properties>  type any,
                   <property>    type any,
                   <id>          type string,
                   <type>        type string,
                   <cardinality> type string,
                   <value_tab>   type any table.

assign lo_json_data->* to <data>.
        assign component 'OBJECTS' of structure <data> to <data2>.

        if sy-subrc = 0 and <data2> is assigned.

          loop at <data2> assigning <objects>.
            assign component 'OBJECT' of structure <objects> to <object>.

            if sy-subrc = 0 and <object> is assigned.
              assign component 'PROPERTIES' of structure <object> to <properties>.

              if sy-subrc = 0 and <properties> is assigned.
                do.
                  lv_count = lv_count + 1.
                  assign component lv_count of structure <properties> to <property>.
                  if sy-subrc <> 0 or  not <property> is assigned.
                    exit.
                  else.
                    assign component 'ID'          of structure <property> to <id>.
                    assign component 'TYPE'        of structure <property> to <type>.
                    assign component 'CARDINALITY' of structure <property> to <cardinality>.

                    if <cardinality> = 'single'.
                      assign component 'VALUE' of structure <property> to field-symbol(<value>).
                    else.
                      assign component 'VALUE' of structure <property> to <value_tab>.
                    endif.

                    ls_response-id          = <id>.
                    ls_response-type        = <type>.
                    ls_response-cardinality = <cardinality>.
                    if ls_response-cardinality = 'single'.
                      ls_value_tab-tline = <value>.
                      append ls_value_tab to lt_value_tab.
                      ls_response-value = lt_value_tab.
                    else.
                      loop at <value_tab> assigning <value>.
                        ls_value_tab-tline = <value>.
                         append ls_value_tab to lt_value_tab.
                      endloop.
                      ls_response-value = lt_value_tab.
                    endif.
                    clear: lt_value_tab[], ls_value_tab.
                    append ls_response to lt_response.
                  endif.
                enddo.
              endif.
            endif.
          endloop.
​