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: 

Transform ABAP Hierarchy table to nested JSON

0 Kudos

Hi,

I have an internal table, e.g.

ID | ParentID | Name

1 | | One
2 | | Two
3 | 2 | Two.One
4 | 3 | Two.One.One

This should be transformed to JSON like this:

{
"root": {
"id": "1",
"name": "One"
},
{
"id": "2",
"name": "Two",
"children": [
{
"id: "3",
"name": "Two.One",
"children": [
{
"id": "4",
"name": "Two.One.One"

}

]

}
]
}
}

Now I am wondering whether there is already a transformation that does this, or maybe someone can help me to create my own transformation for this.

Thanks a lot

Ole

1 ACCEPTED SOLUTION

Thank you, Sandra, that answer was really helpful.

This is not exactly what I had in mind. The result of the ID transformation is not nested at all, and all the columns are transferred. That's not what I wanted.

But your answer let me have another look at the documentation, and I found this: http://help.sap.com/abapdocu_740/de/abenabap_json_token_writer_abexa.htm

Class CL_SXML_STRING_WRITER can be used to write JSON directly, using methods of interface IF_SXML_WRITER. So for Transformation from ABAP to JSON, I don't need a transformation at all, but I can do this in a class. Recursive method calls will help to build the nested structure. That should definitely work.

2 REPLIES 2

Sandra_Rossi
Active Contributor
0 Kudos

You may use the ID transformation with the source = your internal table, and the result = an SXML instance set for instance with

writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). It's documented in the ABAP official doc. The component names will be upper case. There are a few discussions in the forum about this topic.

Thank you, Sandra, that answer was really helpful.

This is not exactly what I had in mind. The result of the ID transformation is not nested at all, and all the columns are transferred. That's not what I wanted.

But your answer let me have another look at the documentation, and I found this: http://help.sap.com/abapdocu_740/de/abenabap_json_token_writer_abexa.htm

Class CL_SXML_STRING_WRITER can be used to write JSON directly, using methods of interface IF_SXML_WRITER. So for Transformation from ABAP to JSON, I don't need a transformation at all, but I can do this in a class. Recursive method calls will help to build the nested structure. That should definitely work.