2017 May 10 7:24 AM
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.
2017 May 10 10:45 AM
SAP's basics about ABAP and JSON:
https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenabap_json.htm
2017 May 10 10: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.
2017 May 10 10:37 AM
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.
2017 May 12 9:07 AM
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 ?
2017 May 12 1:57 PM
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 ?
2017 May 10 10:45 AM
SAP's basics about ABAP and JSON:
https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenabap_json.htm
2017 May 12 9:30 AM
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.
2019 Jun 20 8:21 PM
Hi Marcus,
Were you able to resolve this issue.I am also facing with similar issue
Regards
Pratik
2020 May 19 5:21 PM
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.