10-26-2016 3:38 PM
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-27-2016 2:15 PM
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.
10-26-2016 5:50 PM
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.
10-27-2016 2:15 PM
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.