3 weeks ago
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:
I assign it to <data>. with assign lo_json_data->* to <data>.
To show the structure of <data> I see the first level of the JSON-structure "OBJECTS"
When clicking on "OBJECTS" I get the second deep structure level "OBJECT":
The deep structure OBJECT again contains the deep structure PROPERTIES:
And the table PROPERTIES contains a undefined number of "basetypes"
<DATA>-OBJECTS[1]-OBJECT-PROPERTIES
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
The VALUE can contain sometimes two ore more values:
<DATA>-OBJECTS[1]-OBJECT-PROPERTIES-%CSECONDARYOBJECTTYPEIDS-VALUE
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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are looking for a bunch of loops, I think:
DATA:
lt_data TYPE ty_table.
FIELD-SYMBOLS:
<objects> TYPE ty_objects,
<object> TYPE ty_object,
<property> TYPE ty_property,
<basetype> TYPE ty_basetype,
<line> TYPE table_line. "ty_table_line.
LOOP AT lt_data-objects ASSIGNING <objects>.
LOOP AT <objects>-object ASSIGNING <object>.
LOOP AT <object>-properties ASSIGNING <property>.
LOOP AT <property>-property ASSIGNING <basetype>.
LOOP AT <basetype>-value ASSIGNING <line>.
ENDLOOP.
ENDLOOP.
ENDLOOP.
ENDLOOP.
ENDLOOP.
You will replace lt_data with <data> in your example.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
75 | |
9 | |
9 | |
8 | |
8 | |
7 | |
7 | |
6 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.