Application Development 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: 

Extract a part of from a JSON Message

ChRa
Explorer
0 Kudos

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",
"source": "https://github.com/cloudevents/spec/pull", "subject": "1234", "data": {
"persons": [ { "firstname": "John", "nachname": "Doe", "age": 46 }, { "vorname": "Jane", "nachname": "Doe", "age": 5 } ] }}

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

1 ACCEPTED SOLUTION

Tukutupap
Participant
0 Kudos

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.

3 REPLIES 3

Tukutupap
Participant
0 Kudos

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.

0 Kudos

Vicente, thanks a lot.

i never tried to work with arrays before.

Many, many Thanks

0 Kudos

Glad you found it useful!