Skip to Content
0

Transform ABAP Hierarchy table to nested JSON

Oct 26, 2016 at 02:38 PM

115

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
avatar image
Former Member
Oct 27, 2016 at 01:15 PM
1

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.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Oct 26, 2016 at 04:50 PM
0

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.

Share
10 |10000 characters needed characters left characters exceeded