2023 Jul 06 10:51 AM
Hi Folks,
I created a very basic simple transformation to convert a flat ABAP structure with 5 fields into XML.
This is the structure:
USERNAME 1 Types ZDE_USERNAME STRING
PASSWORD 1 Types ZDE_PASSWORD STRING
CLIENT_ID 1 Types ZDE_CLIENT_ID STRING
CLIENT_SECRET 1 Types ZDE_CLIENT_SECRET STRING
GRANT_TYPE 1 Types ZDE_GRANT_TYPE STRING
And this is the transformation:
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
<tt:root name="ACCESS_TOKEN" type="ddic:ZSTY_ACCESS_TOKEN"/>
<tt:template>
<access_token>
<username tt:value-ref=".ACCESS_TOKEN.USERNAME"/>
<password tt:value-ref=".ACCESS_TOKEN.PASSWORD"/>
<client_id tt:value-ref=".ACCESS_TOKEN.CLIENT_ID"/>
<client_secret tt:value-ref=".ACCESS_TOKEN.CLIENT_SECRET"/>
<grant_type tt:value-ref=".ACCESS_TOKEN.GRANT_TYPE"/>
</access_token>
</tt:template>
</tt:transform>
I call the transformation:
CALL TRANSFORMATION zca_access_token
SOURCE access_token = ls_data
RESULT XML lv_xstring.
And it works perfectly; I get exactly the XML output intended:
<?xml version="1.0" encoding="utf-8"?>
<access_token>
<username>xxxxxxxxxxxxxxxxxxxxxxxx</username>
<password>yyyyyyyyyyyyyyyyyyyyyyyy</password>
<client_id>vvvvvvvvvvvvvvvvvvvvvvv</client_id>
<client_secret>zzzzzzzzzzzzzzzzzzz</client_secret>
<grant_type>password</grant_type>
</access_token>
So far so good.
Now I want to derive the JSON version of the XML, so I do this:
lr_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION zca_access_token
SOURCE access_token = ls_data
RESULT XML lr_writer.
But it throws an ABAP error:
Executing the 'OpenElement' operation leads to an invalid XML document
Why? (I expect the answer is obvious, but I've never worked with JSON transformations before).
Cheers
John Moulding.
2023 Jul 06 11:13 AM
I am using the following library from the xco libraries in order to generate and read json strings.
This is however only available as of SAP S/4HANA 2021 or in SAP BTP ABAP Environment.
Kind regards,
Andre
2023 Jul 06 11:24 AM
Thank you Andre,
unfortunately (and I should have mentioned this), we're on ERP ECC6.
I think the issue lies with the transformation, which should be specific to JSON-XML, but I can't find any examples...
Cheers
John
2023 Jul 06 11:32 AM
Maybe this blog post contains answers for you?
ABAP: Fast JSON Serialization | SAP Blogs
Though the author 8ae6723ab3fa43a8980099fe48f90b04 reported problems when using the xco libraries he in detail explains all the other options that should be available in your release.
2023 Jul 06 11:56 AM
Hi Andre,
Timo's article is comprehensive and thorough, but it assumes a reasonable knowledge of transformations. Whereas I didn't even know that a JSON transformation had to be different from an XML transformation (I wasn't able to find any basic pointers on creating a simple transformation for JSON output, although I'm sure they're out there).
Anyway, I have an answer. I adapted my original XML transformation (above) to match the relevant lines in DEMO_ST_JSON_TABLE; this is the new JSON-specific version:
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary">
<tt:root name="ACCESS_TOKEN" type="ddic:ZSTY_EDOC_ACCESS_TOKEN_IN"/>
<tt:template>
<object>
<str name="username">
<tt:value ref="access_token.username"/>
</str>
<str name="password">
<tt:value ref="access_token.password"/>
</str>
<str name="client_id">
<tt:value ref="access_token.client_id"/>
</str>
<str name="client_secret">
<tt:value ref="access_token.client_secret"/>
</str>
<str name="grant_type">
<tt:value ref="access_token.grant_type"/>
</str>
</object>
</tt:template>
</tt:transform>
And it works.
Thanks to everyone who read this, and I hope it helps another Newbie out there.
John