SXI Forum

A place to collect usefull tips, tricks and implementation strategies.

You are not logged in.

#1 20-07-2021 08:52:46

SeanR
Administrator
Registered: 20-11-2018
Posts: 148

Correct JSON format from XML Stylesheet for RESTConnector

On occasion a REST API requires JSON in a certain format and this can be a source of frustration.  Especially if they require an array with only on element.

E.g.

{"SomeArray":["abc", 2]}

The RESTConnector will convert XML to JSON before ending the payload to the API.  Therefore the above example can be easily achieved by configuring a stylesheet and giving the XML elements certain attributes to define the JSON output.

Below is an example XML:

<MyElement class="Object">
    <myName type="string">json</myName >
    <myBool type="boolean">true</myBool >
    <myInt type="number">3</myInt >
    <anArrayElement class="Array">
        <myVal type="number">4</myVal>
        <myVal type="number">5</myVal>
    </anArrayElement>
</MyElement>

This is what the output will look like:

{
    "MyElement": {
        "myName": "json",
        "myBool": true,
        "myInt": 3,
        "anArrayElement": [ 4, 5 ]
    }
}

If you wish to force an array with a single element you can simply make you xml look like this:

    <myElement class="Array"><myVal>SomeValue</myVal></MyElement>

And the following JSON will be produced:

    { "myElement":[ "SomeValue" ] }

The full technical doc can be found here

Offline

Board footer

Powered by FluxBB