
In my admittedly rather short blog http://scn.sap.com/community/abap/blog/2013/01/07/abap-and-json I mentioned JSON-XML, that you have to know if you want to write transformations or serializations for JSON. The background is that transformations and serializations don't know JSON but only XML. The trick is that a transformation called with CALL TRANSFORMATION that has a JSON-writer as result must produce JSON-XML and that the writer renders it to JSON. A JSON-reader as transformation source does it another way around by producing JSON-XML from a JSON input and passing that to a transformation. So, if you want to transform ABAP data with a selfdefined transformation to JSON: An XSLT must transform the intermediate asXML to JSON-XML and an ST must produce JSON-XML. Therefore, let's have look at JSON-XML.
JSON XML is an SAP-specific representation of JSON data in XML format. The single values, arrays and objects in JSON are represented as follows in XML:
"..." → <str>...</str>
... → <num>...</num>
true → <bool>true</bool>
false → <bool>false</bool>
null → <null />
[...] → <array>...</array>
The components of arrays, separated by commas, are mapped as subelements of the element <array>, where the type-specific mapping rule applies to each element.
{...} →<object>...</object>
The components of objects, separated by commas, are mapped as subelements of the element <object>. There are two representation methods:
In the shorter method, used by default, a component is mapped like an element of an array, with the name n added to the type-specific XML element of the component as the content of the attribute name. In the second longer method, the type-specific XML element of a component is nested in an additional element <member>, which then has the attribute name with the name of the component.
In the longer alternative for object components, each component in JSON-XML is identified clearly by a <member> element. This can make it easier to distinguish objects of arrays if only partial fragments of JSON data are being edited.
A JSON writer that renders JSON-XML to JSON accepts both alternatives for object components. A JSON reader that parses JSON data to JSON-XML creates the shorter variant by default. To create the longer variant with <member> elements, the method SET_OPTION of the interface IF_SXML_READER can be used to set the option IF_SXML_READER=>CO_OPT_SEP_MEMBER.
"abcde"
<str>abcde</str>
1.234e+5
<num>1.234e+5</num>
true
<bool>true</bool>
false
<bool>false</bool>
null
<null/>
[]
<array/>
[
"abcde",
1.234e+5,
null
]
<array>
<str>abcde</str>
<num>1.234e+5</num>
<null/>
</array>
[
[
"abcde",
1.234e+5,
true
],
[
"fghij",
1.234e+5,
false
]
]
<array>
<array>
<str>abcde</str>
<num>1.234e+5</num>
<bool>true</bool>
</array>
<array>
<str>fghij</str>
<num>1.234e+5</num>
<bool>false</bool>
</array>
</array>
{}
<object/>
{
"text":"abcde",
"number":1.234e+5,
"unknown":null
}
<object>
<str name="text">abcde</str>
<num name="number">1.234e+5</num>
<null name="unknown"/>
</object>
{
"obj1":
{
"text":"text",
"number":1.234e+5,
"flag":true
},
"obj2":
{
"text":"text",
"number":1.234e+5,
"flag":false
}
}
<object>
<object name="obj1">
<str name="text">text</str>
<num name="number">1.234e+5</num>
<bool name="flag">true</bool>
</object>
<object name="obj2">
<str name="text">text</str>
<num name="number">1.234e+5</num>
<bool name="flag">false</bool>
</object>
</object>
{
"Title":"abcde",
"Date":"2013-04-11",
"Time":"08:36:44",
"Amount":"111",
"Object":
{
"Name":"fghij",
"Flag":true,
"Array1":
[
"comp1",
"comp2",
"comp3"
],
"Array2":
[
{
"attr1":111,
"attr2":222
},
[
333,
444
]
],
"Array3":
[],
"Significance":null
}
}
<object>
<str name="Title">abcde</str>
<str name="Date">2013-04-11</str>
<str name="Time">08:36:44</str>
<str name="Amount">111</str>
<object name="Object">
<str name="Name">fghij</str>
<bool name="Flag">true</bool>
<array name="Array1">
<str>comp1</str>
<str>comp2</str>
<str>comp3</str>
</array>
<array name="Array2">
<object>
<num name="attr1">111</num>
<num name="attr2">222</num>
</object>
<array>
<num>333</num>
<num>444</num>
</array>
</array>
<array name="Array3"/>
<null name="Significance"/>
</object>
</object>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
4 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |