JSONx conversion rules
A DataPower® Gateway applies rules to convert JSON payloads to JSONx.
A DataPower Gateway applies the following rules to convert JSON payloads
(RFC 4627) to JSONx.
- root element
- The root element is either a
<json:object>or a<json:array>element and has the following declaration.<… xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"> - object
- A JSON object becomes a
<json:object>element. If the object has a property name, JSONx encodes anameattribute whose value is assigned the property name. The value of the object depends on its type. The following table shows a sample JSON object conversion to JSONx.Table 1. Conversion of a JSON object to JSONx. JSON JSONx { "Ticker" : "IBM" }<json:object> <json:string name="Ticker">IBM</json:string> </json:object> - array
- A JSON array becomes a
<json:array>element. If the array has a property name, JSONx encodes anameattribute whose value is assigned the property name. The value of the array depends on its type. Array elements are ordered according to their document order. The following table shows a sample JSON array conversion to JSONx.Table 2. Conversion of a JSON array to JSONx. JSON JSONx "phoneNumbers": [ "212 555-1111", "212 555-2222" ]<json:array name="phoneNumbers"> <json:string>212 555-1111</json:string> <json:string>212 555-2222</json:string> </json:array> - Boolean
- A JSON Boolean becomes a
<json:boolean>element. If the Boolean has a property name, JSONx encodes anameattribute whose value is assigned the property name. The Boolean value is character data as eithertrueorfalse. The following table shows a sample JSON Boolean conversion to JSONx.Table 3. Conversion of a JSON Boolean to JSONx. JSON JSONx "remote": false<json:boolean name="remote">false</json:boolean> - string
- A JSON string becomes a
<json:string>element. If the string has a property name, JSONx encodes anameattribute whose value is assigned the property name. The string value is character data. The following table shows a sample JSON string conversion to JSONx.Table 4. Conversion of a JSON string to JSONx. JSON JSONx "name":"John Smith"<json:string name="name">John Smith</json:string> - number
- A JSON number becomes a
<json:number>element. If the number has a property name, JSONx encodes anameattribute whose value is assigned the property name. The number value is character data. The following table shows a sample JSON number conversion to JSONx.Table 5. Conversion of a JSON number to JSONx. JSON JSONx "height": 62.4<json:number name="height">62.4</json:number> - null
- A JSON value of
nullbecomes a<json:null>element. If the null value has a property name, JSONx encodes anameattribute whose value is assigned the property name. The following table shows a sample JSON null conversion to JSONx.Table 6. Conversion of a JSON null to JSONx. JSON JSONx "additionalInfo": null<json:null name="additionalInfo" /> - Special characters and escaped characters
- JSON escaped characters in strings might become character entity references. Any strings escaped
with
\ubecome XML entity references. During processing, the JSON parser converts\ustrings to the hex formatwhere&#xhhhh;
is a hex number. The resultant JSONx XML entity reference is specified with the decimal formathhhh
where&#nnnnn;
is a decimal number.nnnnn
The
\n(line feed),\r(carriage return), and\t(tab) characters are supported XML 1.0 characters. The\b(backspace) and the\f(form feed) characters are not supported in XML. Therefore, these characters are not supported in JSONx.The parser might not convert label values and string values into equivalent JSONx. The following table shows examples of special characters and escaped characters as label values and as string values converted to JSONx.Table 7. Conversion of JSON characters to JSONx. JSON JSONx { "&" : "&"}<json:string name="&">&</json:string>{"<" : "<"}<json:string name="<"><</json:string>{">" : ">"}<json:string name=">">></json:string>{ "\"" : "\""}<json:string name=""">"</json:string>{"'" : "'"}<json:string name="'">'</json:string>{"\u005c" : "\u005c"}<json:string name="\">\</json:string>{"\u0062" : "\u0062" }<json:string name="b">b</json:string>When a special character is transformed from JSON to JSONx and back to JSON, the resultant character might not be identical to the original JSON. In the following example, the string value contains Unicode with an escape character for the copyright symbol.
The transformed JSONx contains the XML entity reference for the copyright symbol in decimal format,{"label":"An escape character for the copyright symbol: \uC2A9"}슩.
When transformed back to JSON, instead of the escape character, the string contains the copyright symbol.<json:string name="label">An escape character for the copyright symbol: 슩</json:string>{"label":"Unicode with an escape character for the copyright symbol: ©"}