I would like to deserialize a JSON string to a structure defined like this:
TYPES: BEGIN OF json_subobject,
c TYPE i,
d TYPE decfloat34,
END OF json_subobject.
TYPES: BEGIN OF json_object,
a TYPE c LENGTH 10,
b TYPE json_subobject,
END OF json_object.
DATA: foo TYPE json_object.
I model this structure in JSON like this:
{
"a":"FooBar",
"b":{
"c":9,
"d":3.14
}
}
Now, I wrote a simple program to try out the CALL TRANSFORMATION statement on the above JSON accompanied with the above structure definitions. The program should deserialize the JSON (hard-coded into the variable lv_xmls) and print out the contents of the resulting structure. The contents of the structure should match the contents of the original JSON. This is the program:
TYPES: BEGIN OF json_subobject,
c TYPE i,
d TYPE decfloat34,
END OF json_subobject.
TYPES: BEGIN OF json_object,
a TYPE c LENGTH 10,
b TYPE json_subobject,
END OF json_object.
DATA: foo TYPE json_object.
DATA: lv_xmls TYPE string VALUE '{"a":"FooBar","b":{"c":9,"d":3.14}}',
lv_xmlb TYPE xstring.
TRY.
lv_xmlb = cl_abap_codepage=>convert_to(
source = lv_xmls
codepage = `UTF-8`
endian = space
replacement = '#'
ignore_cerr = abap_false ).
CATCH cx_parameter_invalid_range cx_sy_codepage_converter_init cx_sy_conversion_codepage cx_parameter_invalid_type.
ENDTRY.
WRITE: |Deserializing JSON ...|.
NEW-LINE.
CALL TRANSFORMATION id SOURCE XML lv_xmlb RESULT XML = foo.
WRITE: '{'.
NEW-LINE.
WRITE: | "a": "{ foo-a }",|.
NEW-LINE.
WRITE: | "b": \{|.
NEW-LINE.
WRITE: | "c": { foo-b-c },|.
NEW-LINE.
WRITE: | "d": { foo-b-d },|.
NEW-LINE.
WRITE: | \}|.
NEW-LINE.
WRITE: |\}|.
The output I would expect is:
Deserializing JSON ...
{
"a":"FooBar",
"b":{
"c":9,
"d":3.14
}
}
But unfortunately, the output I get is:
Deserializing JSON ...
{
"a":"",
"b":{
"c":0,
"d":0
}
}
It looks like the CALL TRANSFORMATION statement isn't doing anything to me.
Is there an ABAP guru who could show me how to use CALL TRANSFORMATION in this (hopefully) simple case? I already looked around and found this helpful GitHub repo demonstrating how to deserialize JSON to a class, but I'd prefer to deserialize JSON to a structure for simplicity. I'm not entirely sure how to use CALL TRANSFORMATION because I'm unfamiliar with XSLT and advanced XML features, so I would very much appreciate a ready-made solution if possible ...
Thanks a lot in advance,
Joshua