/ui2/cl_json=>deserialize doesn't fill structure - json

I have two types of JSON: result and error.
I'm trying to deserialize these two JSON to my internal structure, but there's a problem.
Only for result the function works correctly, for error the structure is always blank.
Can anybody help me to solve my problem or indicate my mistake?
Here are my JSON:
{
"result": [
{
"to": "to_somebody",
"id": "some_id",
"code": "some_code"
}
]
}
{
"error": {
"name": "some_name",
"date": [],
"id": "11",
"descr": "Unknown error"
},
"result": null
}
Here is my ABAP code (there's a screen to enter the JSON):
DATA go_textedit TYPE REF TO cl_gui_textedit.
PARAMETERS: json TYPE string.
AT SELECTION-SCREEN OUTPUT.
IF go_textedit IS NOT BOUND.
CREATE OBJECT go_textedit
EXPORTING
parent = cl_gui_container=>screen0.
go_textedit->set_textstream( json ).
ENDIF.
AT SELECTION-SCREEN.
go_textedit->get_textstream( IMPORTING text = json ).
cl_gui_cfw=>flush( ).
TYPES: BEGIN OF stt_result,
to TYPE string,
id TYPE string,
code TYPE string,
END OF stt_result.
TYPES: BEGIN OF stt_error,
name TYPE string,
date TYPE string,
id TYPE string,
descr TYPE string,
result TYPE string,
END OF stt_error.
DATA: BEGIN OF ls_response_result,
result TYPE STANDARD TABLE OF stt_result,
error TYPE STANDARD TABLE OF stt_error,
END OF ls_response_result.
/ui2/cl_json=>deserialize( EXPORTING json = lv_cdata
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = ls_response_result ).

Revise your type declarations. There is a discrepancy in one place where you expect result as a JSON array (ABAP table) vs. a JSON object (ABAP structure).
This is the complete code I used:
CLASS the_json_parser DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
TYPES:
BEGIN OF result_structure,
to TYPE string,
id TYPE string,
code TYPE string,
END OF result_structure.
TYPES result_table TYPE
STANDARD TABLE OF result_structure
WITH EMPTY KEY.
TYPES date_table TYPE
STANDARD TABLE OF string
WITH EMPTY KEY.
TYPES:
BEGIN OF error_structure,
name TYPE string,
date TYPE date_table,
id TYPE string,
descr TYPE string,
result TYPE string,
END OF error_structure.
TYPES:
BEGIN OF complete_result_structure,
result TYPE result_table,
error TYPE error_structure,
END OF complete_result_structure.
CLASS-METHODS parse
IMPORTING
json TYPE string
RETURNING
VALUE(result) TYPE complete_result_structure.
ENDCLASS.
CLASS the_json_parser IMPLEMENTATION.
METHOD parse.
/ui2/cl_json=>deserialize(
EXPORTING
json = json
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING
data = result ).
ENDMETHOD.
ENDCLASS.
Verified with the test class:
CLASS unit_tests DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PUBLIC SECTION.
METHODS parses_result FOR TESTING.
METHODS parses_error FOR TESTING.
ENDCLASS.
CLASS unit_tests IMPLEMENTATION.
METHOD parses_result.
DATA(json) = `{` &&
`"result": [` &&
`{` &&
`"to": "to_somebody",` &&
`"id": "some_id",` &&
`"code": "some_code"` &&
`}` &&
`]` &&
`}`.
DATA(result) = the_json_parser=>parse( json ).
cl_abap_unit_assert=>assert_not_initial( result ).
cl_abap_unit_assert=>assert_not_initial( result-result ).
cl_abap_unit_assert=>assert_initial( result-error ).
ENDMETHOD.
METHOD parses_error.
DATA(json) = `{` &&
`"error": {` &&
`"name": "some_name",` &&
`"date": [],` &&
`"id": "11",` &&
`"descr": "Unknown error"` &&
`},` &&
`"result": null` &&
`}`.
DATA(result) = the_json_parser=>parse( json ).
cl_abap_unit_assert=>assert_not_initial( result ).
cl_abap_unit_assert=>assert_initial( result-result ).
cl_abap_unit_assert=>assert_not_initial( result-error ).
ENDMETHOD.
ENDCLASS.

A JSON object {...} can be mapped only by an ABAP structure.
A JSON array [...] can be mapped only by an ABAP internal table.
In your code, the error JSON is a JSON object, but the ABAP variable is an internal table.
So you should correct the ABAP variable by removing STANDARD TABLE OF so that it becomes a structure:
DATA: BEGIN OF ls_response_result,
result TYPE STANDARD TABLE OF stt_result,
error TYPE stt_error, " <=== "STANDARD TABLE OF" removed
END OF ls_response_result.

Thanks to all for your advice.
I solve my problem.
The case was:
I get from provider two types of JSON: result or error.
I can't get both of them at the same time.
I need to deserialize them to my internal structure.
{
"result": [
{
"to": "some_value",
"id": "some_id",
"code": "some_code"
}
]
}
and
{
"error": {
"name": "some_name",
"date": [some_date],
"id": "some_id",
"descr": "some_description"
},
"result": null
}
Here's my needed ABAP code, which works correctly.
P.S: My mistake was that I worked with error like with an internal table instead of structure.
TYPES: BEGIN OF stt_result,
to TYPE string,
id TYPE string,
code TYPE string,
END OF stt_result.
TYPES lt_data TYPE STANDARD TABLE OF string WITH EMPTY KEY.
TYPES: BEGIN OF stt_error,
name TYPE string,
date TYPE lt_data,
id TYPE string,
descr TYPE string,
result TYPE stt_result,
END OF stt_error.
DATA: BEGIN OF ls_response_result,
result TYPE STANDARD TABLE OF stt_result,
error TYPE stt_error,
END OF ls_response_result.
/ui2/cl_json=>deserialize( EXPORTING json = lv_cdata
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = ls_response_result ).

Related

Parsing json using deserialize in abap btp environment

I fetched the data from a URL and data is stored in a variable in json format.now i need to parse this json format but i am unable to parse the data.
below is the code
CLASS zcode_82 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
DATA:lv_response TYPE string,
lv_body type string,
lv_path type string,
lv_json TYPE /ui2/cl_json=>json,
r_json type string.
types: begin of ty_data,
field type string,
id type string,
customer type string,
customer_id type string,
address type string,
date_Created type string,
time_created type string,
END OF TY_DATA.
data: lv_data type STANDARD TABLE OF ty_data with DEFAULT KEY,
lr_data TYPE REF TO data,
ls_data type ty_data.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcode_82 IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA: lv_body_1 TYPE string,
ls_value type RANGE OF ty_data.
TRY.
"create http destination by url; API endpoint for API sandbox
DATA(lo_http_destination) =
cl_http_destination_provider=>create_by_url( 'enter the url ' ).
"create HTTP client by destination
DATA(lo_web_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ) .
"adding headers with API Key for API Sandbox
DATA(lo_web_http_request) = lo_web_http_client->get_http_request( ).
lo_web_http_request->set_header_fields( VALUE #(
( name = 'Authorization' value = 'Bearer key' )
( name = 'Content-Type' value = 'application/json' )
) ).
"set request method and execute request
DATA(lo_web_http_response) = lo_web_http_client->execute( if_web_http_client=>get ).
lv_response = lo_web_http_response->get_text( ).
CATCH cx_http_dest_provider_error cx_web_http_client_error cx_web_message_error.
"error handling
ENDTRY.
* out->write( |response: { lv_response }| ).
*
CLEAR lv_data[].
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_Response
* jsonx =
pretty_name = /ui2/cl_json=>pretty_mode-user
* assoc_arrays =
* assoc_arrays_opt =
* name_mappings =
* conversion_exits =
* hex_as_base64 =
CHANGING
data = lv_data
).
out->write(
EXPORTING
data = lv_data
* name =
* RECEIVING
* output =
).
endmethod.
endclass.
Here the downloaded data from url is the input and the input is
response: {"records":[{"id":"rec5Qk24OQpKDyykq","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010001","address":"Chennai","created_time":"06:00:14","customer":"IDADMIN","date_created":"16.04.2004"}},{"id":"rec7bSe8Zb18z6b5a","createdTime":"2022-08-08T13:07:16.000Z","fields":{"customer_id":"0000010007","address":"Kakinada","created_time":"04:01:18","customer":"Ramya","date_created":"15.04.2000"}},{"id":"recD9Hh4YLgNXOhUE","createdTime":"2022-08-08T11:48:06.000Z","fields":{"customer_id":"0000010002","address":"Bangalore","created_time":"04:03:35","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recK7Tfw4PFAedDiB","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010005","address":"Chennai","created_time":"06:00:49","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recKOq0DhEtAma7BV","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010006","address":"Hyderabad","created_time":"18:42:28","customer":"GLAESS","date_created":"21.04.2004"}},{"id":"recS8pg10dFBGj8o7","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010003","address":"Gurugram","created_time":"04:10:02","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recf4QbOmKMrBeLQZ","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010004","address":"Bangalore","created_time":"06:00:12","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recs7oHEqfkN87`enter code here`tWm","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010000","address":"Hyderabad","created_time":"04:01:18","customer":"MAASSBERG","date_created":"15.04.2004"}}]}
The output of the code is
Table
FIELD ID CUSTOMER CUSTOMER_ID ADDRESS DATE_CREATED TIME_CREATED
Your structure type simply does not match the JSON structure. You can't simply skip the outer records array and expect the deserializer to know you want to deserialize what is within it. Same with the fields object to string, it's an object/structure, not a string.
The JSON <> ABAP type would look like this
{
"records":[
{
"id":"rec5Qk24OQpKDyykq",
"createdTime":"2022-08-03T10:14:43.000Z",
"fields":{
"customer_id":"0000010001",
"address":"Chennai",
"created_time":"06:00:14",
"customer":"IDADMIN",
"date_created":"16.04.2004"
}
},
...
TYPES: BEGIN OF ty_field,
customer_id TYPE string,
address TYPE string,
created_time TYPE string,
customer TYPE string,
date_created TYPE string,
END OF ty_field.
TYPES: BEGIN OF ty_record,
id TYPE string,
createdtime TYPE string,
fields TYPE ty_field,
END OF ty_record.
TYPES tt_record TYPE STANDARD TABLE OF ty_record WITH EMPTY KEY.
TYPES: BEGIN OF ty_response,
records TYPE tt_record,
END OF ty_response.
Then you can use a variable like DATA ls_response TYPE ty_response as your data parameter.
Side note: If you can, consider using Simple Transformations with JSON data. There you can fine-tune namings (mix snake_case and camelCase), which fields to serialize, deserialze, require/optional, and it's magnitudes faster (especially with larger json files).

how to get value from json via variable in typescript?

supposing I have to read some data from some json files(i18n), every json file may look like:
{
"foo": "1",
"bar": "2",
...
}
I don't know how many fields this json have(it can be expanded), but it's fields look like
{
[prop: string]: string
}
besides, all the json files share the same fields.
when I try to read a value from this json via:
//a can be expanded, I'm not sure how many fileds does it have
let a = {
name: "dd",
addr: "ee",
}
//I'm confident a has a field "name"
let b = "name";
console.log(a[b]);
the error message is:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
how could I fix it?
The error you're encountering is because the keys in a is not just any string (in fact, it can only be "name" or "add"), but b can be a string of any arbitrary value. If you are very sure that b represents a key found in the object a, you can hint TypeScript as such:
let b: keyof typeof a = "name";
Attempting to assign any arbitrary string value to b will lead to an error:
// This will cause an error
let b: key typeof a = "foobar";
See proof-of-concept on TypeScript Playground.

Golang Error Types are empty when encoded to JSON

I'm trying to encode some JSON for a REST api, everything is working fine except for some errors. For example, with this struct:
type TemplateResponse struct {
Message string
Error error
Template Template
}
Encoded with this data:
res := TemplateResponse{"Template not found.", fmt.Errorf("There is no template on this host with the name " + vars["name"]), Template{}}
json.NewEncoder(w).Encode(res)
Returns:
{
"Message": "Template not found.",
"Error": {},
"Template": {
"Name": "",
"Disabled": false,
"Path": "",
"Version": ""
}
}
I'm getting this seemingly randomly across my application, where 'error' types are being returned as empty. Any ideas?
Thanks!
Because error is just an interface. It may hold a value of any concrete type that implements it.
In your example you used fmt.Errorf() to create an error value. That calls errors.New() which returns a pointer to a value of the unexported errors.errorString struct. Its definition is:
type errorString struct {
s string
}
This struct value will be marshaled, but since it has no exported fields (only exported fields are marshaled), it will be an empty JSON object: {}.
The "fix" is: don't marshal values of "general" interfaces, relying on that the dynamic values can be marshaled into JSON meaningfully. Instead you should add a field that stores the error string (the result of error.Error()), and omit the Error error field from marshaling, e.g.:
type TemplateResponse struct {
Message string
Error error `json:"-"`
ErrorMsg string
Template Template
}
Of course then you also need to set / fill the ErrorMsg field before marshaling.
Or if you don't need to store the error value in the struct, remove that field completely:
type TemplateResponse struct {
Message string
ErrorMsg string
Template Template
}
If you still want to keep the Error error field (and not the ErrorMsg field), then you need to implement a custom marshaling logic by implementing the json.Marshaler interface where you can "convert" the error value to a meaningful string for example (or into another value that can be marshaled properly).

What kind of data structure is needed to parse JSON to itab?

I want to parse a json string into an abap internal table, for example, this one
{
"apiVersion": "1.0",
"data": {
"location": "Dresden",
"temperature": "7",
"skytext": "Light rain",
"humidity": "96",
"wind": "7.31 km/h",
"date": "02-14-2017",
"day": "Tuesday"
}
}
I want to use the method cl_fdt_json=>json_to_data and put the values and keys into a table like this
types: begin of map,
key type string,
value type string,
end of map.
data json_data type standard table of map.
But, unfortunately, it does not work like that. Does anyone have experience with this kind of problem? I don't have access to all the documentation because this is my sample task for a hirement to SAP and this is the last part of the "puzzle" ;) It is hard for me to find the solution.
Thank you!!!
EDIT: accordingly to vwegerts answer I tried the following. This is a little bit different to what i originally wanted to do, but it would also be ok)
DATA cl_oops TYPE REF TO cx_dynamic_check.
DATA(text) = result.
TYPES: BEGIN OF ty_structure,
skytext TYPE string,
location type string,
temperature type string,
humidity type string,
wind type string,
date type string,
day type string,
END OF ty_structure.
DATA : wa_structure TYPE ty_structure.
TRY.
CALL TRANSFORMATION id
SOURCE XML text
RESULT data = wa_structure.
message wa_structure-skytext type 'I'.
CATCH cx_transformation_error INTO cl_oops.
WRITE cl_oops->get_longtext( ).
ENDTRY.
but it still doesnt work. when i check the value of wa_structure-skytext it is unfortunatly empty. i cannot find the mistake. does anyone have an idea?
Rather than use the FDT class (which might not be available on all systems), you might want to take a look at the well-documented capabilities of the ABAP runtime system itself. This example program might be the way to go for you. You would essentially provide a Simple Transformation that would map the JSON XML structure to your data structure, instantiate a sXML JSON reader and then pass that as source to CALL TRANSFORMATION.
Besides #vwegert recommendation to use the SAP documented json transformations, you could check the open source alternatives. This one looks promising.
{"apiVersion":"1.0", "data":{ "location":"Dresden", "temperature":"7", "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } }
The corresponding structure in ABAP would be:
"The nested data table
Types: Begin of ty_data,
location TYPE string,
temperature TYPE string,
skytext TYPE string,
etc.
End of ty_data,
ty_t_data TYPE STANDARD TABLE OF ty_data WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 0.
"the whole json structure
Types: Begin of ty_json,
apiversion TYPE string,
data TYPE ty_t_data,
End of ty_json.
DATA: ls_data TYPE ty_json.
Now you have to find a proper JSON deserializer, which handles nested tables.
Most deserializer expect a table input, so you have to add '['... ']' at the end of your JSON string and define lt_data TYPE STANDARD TABLE OF ty_json.
You can do it like that via SAP JSON-XML reader:
CLASS lcl_json DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF map,
key TYPE string,
value TYPE string,
END OF map,
tt_map TYPE STANDARD TABLE OF map WITH DEFAULT KEY.
CLASS-METHODS: parse IMPORTING iv_json TYPE string
RETURNING VALUE(rv_map) TYPE tt_map.
ENDCLASS.
CLASS lcl_json IMPLEMENTATION.
METHOD parse.
DATA(o_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( iv_json ) ).
TRY.
DATA(o_node) = o_reader->read_next_node( ).
WHILE o_node IS BOUND.
CASE o_node->type.
WHEN if_sxml_node=>co_nt_element_open.
DATA(op) = CAST if_sxml_open_element( o_node ).
LOOP AT op->get_attributes( ) ASSIGNING FIELD-SYMBOL(<a>).
APPEND VALUE #( key = <a>->get_value( ) ) TO rv_map ASSIGNING FIELD-SYMBOL(<json>).
ENDLOOP.
WHEN if_sxml_node=>co_nt_value.
DATA(val) = CAST if_sxml_value_node( o_node ).
<json>-value = val->get_value( ).
WHEN OTHERS.
ENDCASE.
o_node = o_reader->read_next_node( ).
ENDWHILE.
CATCH cx_root INTO DATA(e_txt).
RAISE EXCEPTION TYPE cx_sxml_parse_error EXPORTING error_text = e_txt->get_text( ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(json_string) = ` {"apiVersion":"1.0", ` &&
` "data":{ "location":"Dresden", "temperature":"7",` &&
` "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } } `.
TRY.
DATA(it_map) = lcl_json=>parse( json_string ).
CATCH cx_root INTO DATA(e_txt).
" do handling
ENDTRY.

Testing the Go model

So this is one of my Go models:
type ObjectReference struct {
IRI string `json:"iri" bson:"iri"`
ObjectType string `json:"objectType" bson:"objectType,omitempty"`
ActivityType string `json:"activityType,omitempty" bson:"activityType,omitempty"`
Errors `bson:"-"`
}
I have a validation on the ActivityType as:
objTypeSuccess := o.ObjectType == "activity"
success = success && objTypeSuccess
if (!objTypeSuccess) {
o.errors = append(o.errors, "Object objectType supplied : " + o.ObjectType + " is invalid. Valid object types are : [activity]")
}
where
o *ObjectReference is in the func() definition
I am trying to write a test as so:
testObj = models.ObjectReference{
// Invalid Obj Type
IRI: "http://localhost:8001/launched",
ObjectType: ????,
ActivityType: testObjType,
}
I don't quite understand how I could initialize the ObjectType in my testObj. Can someone help me with this?
The ObjectReference.ObjectType is a field of type string. That being said you can initialize it with an expression of type string. The most basic/simple expression of type string is a string literal such as "hello".
Since in your test code you compare it to the value "activity", I assume that is what you want to initialize it with. You can do that like this:
testObj = models.ObjectReference{
IRI: "http://localhost:8001/launched",
ObjectType: "activity",
ActivityType: testObjType,
}
But of course you can specify any other values/expressions of type stirng:
testObj = models.ObjectReference{
IRI: "http://localhost:8001/launched",
ObjectType: "NOT-AN-ACTIVITY",
ActivityType: testObjType,
}