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

Complex JSON structure to ABAP

marcus_schiffer
Active Participant
0 Kudos
12,158

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.

1 ACCEPTED SOLUTION
8 REPLIES 8
Read only

vladimir_erakovic
Contributor
3,035

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.

Read only

0 Kudos
3,035

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.

Read only

0 Kudos
3,035

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 ?

Read only

0 Kudos
3,035

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 ?

Read only

matt
Active Contributor
0 Kudos
3,035

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.

Read only

3,035

Hi Marcus,

Were you able to resolve this issue.I am also facing with similar issue

Regards

Pratik

Read only

0 Kudos
3,035

I did it using json_to_data :

DATA: lo_json TYPE REF TO cl_clb_parse_json .

CREATE OBJECT lo_json.

CALL METHOD lo_json->json_to_data(

EXPORTING

iv_json = p_string

CHANGING

c_data = wa_response ).

Where p_string (TYPE STRINGVAL) is the json. wa_response is the same you did.