Skip to Content

Complex JSON structure to ABAP

Hi,

I am struggling with converting a JSON structure to ABAP data.

The JSON looks like

{
  "_id": "string",
  "error": "string",
  "request": "string", 

  "predictions": [
    {
      "name": "string",
      "results": [         {
          "label": "string",
          "score": 0
        }
      ]
    }
  ],
  "status": "QUEUED",  
  "tenantName": "string",
  "error_description": "string"
}

and I tried with creating a nested ABAP structure like

types: begin of classificationResults_s,
 label type string,
 score type string,
 end of classificationResults_s.


types: results type standard table of classificationResults_s with non-unique default key.


types: begin of prediction_s,
 name type string,
 results type results,
 end of prediction_s.

types: predictions type table of prediction_s with non-unique default key.


TYPES: BEGIN OF response,
              _id TYPE string,
            error TYPE string,
           request type string,
       predictions type predictions,
            status type string,
          tenantName type string,
  error_description type string,
    END OF response.
data: wa_response type response.


CALL METHOD /UI2/CL_JSON=>DESERIALIZE
 EXPORTING
  JSON = response
 CHANGING
  DATA = wa_response

But that never gives me any result.

When I try with a structure containing only one internal table everything works just fine. However, when introducing the second layer (results in this case) the deserialization returns an empty data structure.

Any help is appreciated.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • May 10, 2017 at 09:45 AM
    Add comment
    10|10000 characters needed characters exceeded

  • May 10, 2017 at 09:28 AM

    Hi Marcus,

    It seams to me that your problem is just in camel case: tenantName from JSON should be tenant_name in your ABAP structure and then when calling deserialize

     /ui2/cl_json=>deserialize( EXPORTING json = request_data
                                           pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                                CHANGING  data = ls_transport_task ).

    Also score: 0 in your JSON should be score: "0" when you use string data type in your structure.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      after removing newline and all blanks it works.

      However, I want the blanks becuase the text fields have blanks that are also removed (Model T --> ModelT)

      REPLACE ALL OCCURRENCES OF REGEX '[ ]' IN response WITH ``.
      REPLACE all occurrences of cl_abap_char_utilities=>newline in response with ''.

      Any idea what causes this behaviour and what else I could do to keep the blanks ?