09-12-2022 1:34 PM
Hi experts,
is it possible on a ABAP - Stack to extract a part of a JSON message as string?
let me explain:
This is an example of my stucture.
{ "type": "company.com.process.1",This message is stucture is uses serveral times.
I want to extract the data section.
The result should look like this:
{ "persons": [ { "firstname": "John", "nachname": "Doe", "age": 46 }, { "vorname": "Jane", "nachname": "Doe", "age": 5 }] }Is this possible?
I know
/ui2/cl_json=>deserialize and /ui2/cl_json=>serialize
but i think with them it is not possible.
BR
09-12-2022 2:21 PM
Well no, but thats going to give you a good starting point.
If you do
/ui2/cl_json=>deserialize(
exporting
json = yourJSON
pretty_name = /ui2/cl_json=>pretty_mode-user
assoc_arrays = abap_true
changing
data = yourData
that is going to return a structure, where all the main nodes of your JSON are referencing the next node down, so in your case it would create an internal table with three rows: type, source and data, each of them pointing to their sublevel.
Now with this:
/ui2/cl_data_access=>create( ir_data = yourData iv_component = 'data' )->ref( ).
And then you could do a serialize to turn 'data' back to a JSON string that would contain only whats in 'data'.
Or you could just do it the old and dirty way and look for the starting and ending point of the data substring and then just get that substring into another variable.
Usually when I do this is not to get the data in a string format but to get it in a structure format that I can then add to an internatl able and loop at it and do some other things though.
Good luck.
09-12-2022 2:21 PM
Well no, but thats going to give you a good starting point.
If you do
/ui2/cl_json=>deserialize(
exporting
json = yourJSON
pretty_name = /ui2/cl_json=>pretty_mode-user
assoc_arrays = abap_true
changing
data = yourData
that is going to return a structure, where all the main nodes of your JSON are referencing the next node down, so in your case it would create an internal table with three rows: type, source and data, each of them pointing to their sublevel.
Now with this:
/ui2/cl_data_access=>create( ir_data = yourData iv_component = 'data' )->ref( ).
And then you could do a serialize to turn 'data' back to a JSON string that would contain only whats in 'data'.
Or you could just do it the old and dirty way and look for the starting and ending point of the data substring and then just get that substring into another variable.
Usually when I do this is not to get the data in a string format but to get it in a structure format that I can then add to an internatl able and loop at it and do some other things though.
Good luck.
09-13-2022 10:35 AM
Vicente, thanks a lot.
i never tried to work with arrays before.
Many, many Thanks
09-13-2022 1:53 PM