Skip to Content
0

Complex JSON structure to ABAP

May 10, 2017 at 06:24 AM

1.4k

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Horst Keller
May 10, 2017 at 09:45 AM
2
Show 1 Share
10 |10000 characters needed characters left characters exceeded

I used transformations (as proposed in the link above) to convert JSON to ABAP. There are a few blogs about it and some questions with useful answers.

0
Vladimir Erakovic May 10, 2017 at 09:28 AM
0

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.

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Go through this link:

https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer

Json integer could be ABAP string by this document, but it have examples with hierarchical data.

0

Hi Vladimir,

thanks for the hints. However, I am still not able to convert that JSON to ABAP.

I found my result a bit different from the JSON object definition (see below) But nevertheless, I still see no conversion.

The response JSON is a bit different and looks like this.

{
  "_id": "90807dce-44bd-40d5-861e-88bc85091039", 
  "predictions": [
    {
      "name": "bild.jpg", 
      "results": [
        {
          "label": "grille", 
          "score": 0.227078
        }, 
        {
          "label": "Model T", 
          "score": 0.17371
        }, 
        {
          "label": "sports car", 
          "score": 0.151759
        }, 
        {
          "label": "car wheel", 
          "score": 0.090716
        }, 
        {
          "label": "racer", 
          "score": 0.084748
        }
      ]
    }
  ], 
  "processed_time": "Fri, 12 May 2017 07:25:34 GMT", 
  "request": {
    "files": [
      "bild.jpg"
    ], 
    "options": {}, 
    "tenantName": "imgclassif-tech-user", 
    "texts": []
  }, 
  "status": "DONE", 
  "tenantName": "imgclassif-tech-user"
}

It seems that more than one nested structure is a problem .

I changed structures etc accordingly but no success.

Or is there any other thing to consider ?

0

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 ?

0