cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle generic JSON nodes with XCO?

martinc_mb
Participant
0 Kudos
111

We are calling a service where where we can get get varying JSON payload responses, and then processing the response with the XCO library (xco_cp_json=>data->from_string).

The outer part is common, but the inside can be different. For example:

{
  "data": {
    "items": [ 
      {
       // At this level, there are different fields depending on the call
      }
    ]
  }
}

I would like to get access to the list/table of entities inside the "items" element and forward that to a method that will handle the specific data depending on the situation.

So I would like to define a structure type to handle this, e.g.

    TYPES: BEGIN OF json_payload,
             BEGIN OF data,
               items TYPE ???, "<- what to use here?
               [ OR ]
               items TYPE TABLE OF ???, "<- what to use here?
             END OF data,
           END OF json_payload.

I am sure that one of the previous JSON libraries I used (perhaps it was my own) allowed me to specify a TYPE REF to a JSON node (e.g. CL_JSON_NODE) and after the deserialization, that would contain a reference to that portion of the JSON document that could then be processed further.

Any ideas on how to handle this kind of situation with XCO?

 

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hi, Is there a reasen that you aren't using the class /UI2/CL_JSON? I don't know the class you are using but I think you can assign the returning parameter with the inline declaration? When I am facing such problems I use the classes cl_abap_datadescr, cl_abap_tabledescr... and so on. This allows me to resolve and assign the structures recursively so I can access them. I hope that helps!

martinc_mb
Participant
0 Kudos
Thanks for the hint, I will give it a try. I am working on a BTP ABAP system and was using the XCO libraries because SAP recommends that for ABAP Cloud, as documented here: https://help.sap.com/docs/sap-btp-abap-environment/abap-environment/xco-library. But I will perhaps try use /UI2/CL_JSON instead, seeing as XCO does not seem to support such handling of JSON data.