Skip to Content

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

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:

Single Values

Character-like values

“…” <str>…</str>

Number values

<num>…</num>

Boolean values

true <bool>true</bool>

false <bool>false</bool>

Null values

null <null />

Data Structures

Arrays

[…] <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.

Objects

{…} <object>…</object>

The components of objects, separated by commas, are mapped as subelements of the element <object>. There are two representation methods:

  • “n”:…<… name=”n”>…</…>
  • “n”:…<member name=”n”><…>…</…></member>

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.

Examples for JSON-XML

Character Data

JSON

“abcde”

JSON-XML

<str>abcde</str>

Numeric Data

JSON

1.234e+5

JSON-XML

<num>1.234e+5</num>

Boolean Value

JSON

true

JSON-XML

<bool>true</bool>

Boolean Value

JSON

false

JSON-XML

<bool>false</bool>

Null Value

JSON

null

JSON-XML

<null/>

Empty Array

JSON

[]

JSON-XML

<array/>

Array

JSON

[
“abcde”,
1.234e+5,
null
]

JSON-XML

<array>
<str>abcde</str>
<num>1.234e+5</num>
<null/>
</array>

Nested Array

JSON

[
[
  “abcde”,
  1.234e+5,
  true
],
[
  “fghij”,
  1.234e+5,
  false
]
]

JSON-XML

<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>

Empty Object

JSON

{}

JSON-XML

<object/>

Object

JSON

{
“text”:”abcde”,
“number”:1.234e+5,
“unknown”:null
}

JSON-XML

<object>
<str name=”text”>abcde</str>
<num name=”number”>1.234e+5</num>
<null name=”unknown”/>
</object>

Nested Objects

JSON

{
“obj1”:
{
  “text”:”text”,
  “number”:1.234e+5,
  “flag”:true
},
“obj2”:
{
  “text”:”text”,
  “number”:1.234e+5,
  “flag”:false
}
}

JSON-XML

<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>

Object with various Components

JSON

{
“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
}
}

JSON-XML

<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>

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. MUHAMMAD AHMAD ALI

    sir i have the same problam i want to get data from json-xml format into abap.

    how can i get the value.when i am using xslt_tool run time error is comming like

    expected was element-end: “object” [ ] Read was element-start: “num” [ ].

    (0) 

Leave a Reply