Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JSON without field name in Array

dimath72
Explorer
0 Likes
6,490

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
).

1 ACCEPTED SOLUTION
Read only

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
5,211

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.

5 REPLIES 5
Read only

Sandra_Rossi
Active Contributor
5,211

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( ) ).

Read only

0 Likes
5,211

Dear Sandra ,

It was very helpuful answer. But if I have my data in itab?

Now I do it like this:

Read only

Sandra_Rossi
Active Contributor
5,211

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.
Read only

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
5,212

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.

Read only

5,211

Very helpful .

Thanks Alexey...