3
votes

I need to populate tables in ABAP from data received through an API.

I'm using the following ABAP function to populate an existing ABAP table from json.

The JSON is correct, and the Table contains corresponding tables within tables.

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = lt_abap  ).

Running this returns a blank lt_abap table.

When changing the output to be a structure this works fine. But the problem is a need a TABLE, rather than a STRUCTURE for subsequent calls

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = ls_abap

Can anyone suggest a solution o get he JSON into my TABLE?

JSON:

{
    "Id": "1369130",
    "Venueid": "0005",
    "Userid": "1320625",
    "Menuid": "null",
    "Created": "2019-07-29T08:18:35.000+0000",
    "items": [
      {
        "Id": "4255354",
        "Total": "3.10",
        "Price": "2.80",
        "Qty": "1",
        "Orderid": "1369130",
        "Menuitemid": "1447268",
        "Externalid": "",
        "Name": "Breakfast Roll Deal",
        "modifiers": [
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "0000001",
            "Orderitemid": "4255354",
            "Externalid": "1000716",
            "Name": "Bacon and Sausage Corn Topped Roll"
          },
          {
            "Price": "0.30",
            "Qty": "1",
            "Id": "00000002",
            "Orderitemid": "4255354",
            "Externalid": "E1001587",
            "Name": "Extra Sausage"
          },
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "00000003",
            "Orderitemid": "4255354",
            "Externalid": "1000774",
            "Name": "Latte"
          },
          {
            "Price": "0.00",
            "Qty": "1",
            "Id": "00000004",
            "Orderitemid": "4255354",
            "Externalid": "E",
            "Name": "Spread"
          }
        ]
      }
]
  }

Table Structure matches exactly.

2
Please add the Minimal, Reproducible Example: a minimal JSON and the type of the internal table are missing (eventually, minimal means that you create a minimal program with a very simple JSON and a corresponding very simple table type).Sandra Rossi
What you posted is not a table. It is a structure. JSON that can be mapped to a table should begin with [ and end with ].Jagger
@Jagger can you convert your comment into an answer so that I can vote for it? Thx.Sandra Rossi
@SandraRossi Did so. Any updates from SAP on LOOP AT SCREEN? :)Jagger
@Jagger Thx. No SAP news about LOOP AT SCREEN since June 28th, still "in progress"; don't be impatient, it's in my SAP support inbox, I'll post news either bad or good as soon as I get some.Sandra Rossi

2 Answers

5
votes

What you posted is not a table. It is a structure. JSON that can be mapped to a table should begin with [ and end with ].

Here is an example.

REPORT ZZZ.

DATA:
  g_tab_t000 TYPE STANDARD TABLE OF t000 WITH EMPTY KEY.

START-OF-SELECTION.
  /ui2/cl_json=>deserialize( EXPORTING json = '[ { "MANDT": "000" }, 
    { "MANDT": "001" }, { "MANDT": "002" } ]' CHANGING data = g_tab_t000 ).
  BREAK-POINT.
0
votes

The Solution couldn't have been simpler...

I simple deserialized into my structure then APPENDED to my table.

/ui2/cl_json=>deserialize( EXPORTING json = lv_json
                             CHANGING data = lS_abap  ).


APPEND LS_abap TO lt_abap.