‎2022 Mar 04 11:10 AM
Dear all,
I am trying to make a json from a nested table that i have and the problem is that the receiver wants it without name fields in the array.
The problem is only in the array .
My json that i send:

And the json that they want:

My code is the following:
TYPES: BEGIN OF TAGS_IDS,
ID TYPE STRING,
END OF TAGS_IDS,
TAGS TYPE STANDARD TABLE OF TAGS_IDS WITH NON-UNIQUE KEY TABLE_LINE.
TYPES: BEGIN OF ITAB,
CONTAINER_ID(20) TYPE C,
CARRIER_ID(20) TYPE C,
DESCRIPTIVE_NAME(20) TYPE C,
SUBSCRIPTION_TYPE(5) TYPE C,
TAG_IDS TYPE TAGS,
END OF ITAB.
DATA: T_ITAB TYPE STANDARD TABLE OF ITAB WITH NON-UNIQUE KEY
CONTAINER_ID,
LS_WA_ITAB TYPE ITAB,
LS_WA_TAGS_IDS TYPE TAGS_IDS.
...
...
LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_WA_ITAB"
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-LOW_CASE
COMPRESS = ABAP_TRUE
* ASSOC_ARRAYS = ABAP_TRUE
* ASSOC_ARRAYS_OPT = ABAP_TRUE
).
‎2022 Mar 16 11:44 AM
Hi Dimitris,
to get the JSON as you need, using /ui2/cl_json, you have 2 ways
1) To provide IDs use a table of type STRING instead of structure.
E.g instead of this:
TYPES: BEGIN OF TAGS_IDS,
ID TYPE STRING,
END OF TAGS_IDS,
TAGS TYPE STANDARD TABLE OF TAGS_IDS WITH NON-UNIQUE KEY TABLE_LINE.Use this:
TAGS TYPE STANDARD TABLE OF STRING WITH NON-UNIQUE KEY TABLE_LINE.And append IDs directly.
2) Define the table with a unique key, then you can use additional options you have already tried for associative arrays:
TYPES:
BEGIN OF TAGS_IDS,
ID TYPE STRING,
END OF TAGS_IDS,
TAGS TYPE SORTED TABLE OF TAGS_IDS WITH UNIQUE KEY id.
BEGIN OF ITAB,
CONTAINER_ID(20) TYPE C,
CARRIER_ID(20) TYPE C,
DESCRIPTIVE_NAME(20) TYPE C,
SUBSCRIPTION_TYPE(5) TYPE C,
TAG_IDS TYPE TAGS,
END OF ITAB.
DATA: T_ITAB TYPE STANDARD TABLE OF ITAB WITH NON-UNIQUE KEY
CONTAINER_ID,
LS_WA_ITAB TYPE ITAB,
LS_WA_TAGS_IDS TYPE TAGS_IDS.
...
...
LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_WA_ITAB"
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-LOW_CASE
COMPRESS = ABAP_TRUE
ASSOC_ARRAYS = ABAP_TRUE
ASSOC_ARRAYS_OPT = ABAP_TRUE
).
Best regards,
Alexey.
‎2022 Mar 04 12:42 PM
Instead of /UI2/CL_JSON class, use full control/full speed/supported solution.
Would be a XSLT Transformation ZZZ manipulating XML-JSON:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<object>
<str name="request_carrier_code">
<xsl:value-of select="object/str[@name='carrier_id']"/>
</str>
<array name="tag_ids">
<xsl:for-each select="object/array[@name='tag_ids']/object/str[@name='id']">
<num>
<xsl:value-of select="."/>
</num>
</xsl:for-each>
</array>
</object>
</xsl:template>
</xsl:transform>that you call this way:
DATA(input) = `{"container_id":"GLDU7779999","carrier_id":"MSCU","tag_ids":[{"id":"126503"}]}`.
DATA(json_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( input ) ).
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
TRY.
CALL TRANSFORMATION zzz SOURCE XML json_reader RESULT XML json_writer.
CATCH cx_root INTO DATA(error).
ENDTRY.
DATA(output) = cl_abap_codepage=>convert_from( json_writer->get_output( ) ).
‎2022 Mar 04 2:43 PM
Dear Sandra ,
It was very helpuful answer. But if I have my data in itab?
Now I do it like this:

‎2022 Mar 04 3:39 PM
If you go from internal table to JSON, a Simple Transformation ZZZ is a faster (i.e. runtime) option than XSLT, code in tt:template would be:
<array>
<tt:loop ref="ROOT">
<object>
<str name="request_carrier_code" tt:value-ref="CARRIER_ID"/>
<array name="tag_ids">
<tt:loop ref="TAG_IDS">
<num tt:value-ref="ID"/>
</tt:loop>
</array>
</object>
</tt:loop>
</array>And ABAP code:CALL TRANSFORMATION zzz SOURCE root = itab RESULT XML json_writer.
‎2022 Mar 16 11:44 AM
Hi Dimitris,
to get the JSON as you need, using /ui2/cl_json, you have 2 ways
1) To provide IDs use a table of type STRING instead of structure.
E.g instead of this:
TYPES: BEGIN OF TAGS_IDS,
ID TYPE STRING,
END OF TAGS_IDS,
TAGS TYPE STANDARD TABLE OF TAGS_IDS WITH NON-UNIQUE KEY TABLE_LINE.Use this:
TAGS TYPE STANDARD TABLE OF STRING WITH NON-UNIQUE KEY TABLE_LINE.And append IDs directly.
2) Define the table with a unique key, then you can use additional options you have already tried for associative arrays:
TYPES:
BEGIN OF TAGS_IDS,
ID TYPE STRING,
END OF TAGS_IDS,
TAGS TYPE SORTED TABLE OF TAGS_IDS WITH UNIQUE KEY id.
BEGIN OF ITAB,
CONTAINER_ID(20) TYPE C,
CARRIER_ID(20) TYPE C,
DESCRIPTIVE_NAME(20) TYPE C,
SUBSCRIPTION_TYPE(5) TYPE C,
TAG_IDS TYPE TAGS,
END OF ITAB.
DATA: T_ITAB TYPE STANDARD TABLE OF ITAB WITH NON-UNIQUE KEY
CONTAINER_ID,
LS_WA_ITAB TYPE ITAB,
LS_WA_TAGS_IDS TYPE TAGS_IDS.
...
...
LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_WA_ITAB"
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-LOW_CASE
COMPRESS = ABAP_TRUE
ASSOC_ARRAYS = ABAP_TRUE
ASSOC_ARRAYS_OPT = ABAP_TRUE
).
Best regards,
Alexey.
‎2022 Mar 16 11:48 AM