Sending JSON with numeric and guid attribute names

234 Views Asked by At

from BizTalk I have to send an JSON file which looks like this.

[
    {
        "attr": {
            "b587d548-8aa6-42b7-b292-0f3e13452c35": {
                "1": "-2.073420455529934786"
            }
        },
        "guid": "80974561-a449-4a94-8b3e-970822b84406",
        "anotherGuid": "05060c4c-f0af-46b8-810e-30c0c00a379e",
        "lastModified": "2019-11-09T01:44:34.157Z",
        "attributes":
        {
            "4": "2019-11-05T20:30:57.6Z",
            "8": "6",
            "10": "8",
            "13": "7",
            "27": "3",
            ...
        },
        ...
    }
]

In a BizTalk Schema I can't define something like this. The Guid in attr and the number attribute names in attributes aren't fix and could be other values.

I have the idea to implement a custom pipeline component which converts the BizTalk XML to Output JSON. But I have no idea how to solve the problem with the names of the Attributes because these are no valid XML names.

What might be the most elegant kind to solve this problem?

Thanks in advance.

UPDATE with more information

To get an JSON like above the XML has to be look like which is invalid

<root>
    <element>
        <anotherGuid>05060c4c-f0af-46b8-810e-30c0c00a379e</anotherGuid>
        <attr>
            <b587d548-8aa6-42b7-b292-0f3e13452c35>
                <1>-2.073420455529934786</1>
            </b587d548-8aa6-42b7-b292-0f3e13452c35>
        </attr>
        <attributes>
            <10>8</10>
            <13>7</13>
            <27>3</27>
            <4>2019-11-05T20:30:57.6Z</4>
            <8>6</8>
        </attributes>
        <guid>80974561-a449-4a94-8b3e-970822b84406</guid>
        <lastModified>2019-11-09T01:44:34.157Z</lastModified>
    </element>
</root>

To get a valid XML I have to change the invalid elements, i.e. instead of <4 /> maybe <e4 />, <element name="4" /> or something like this. Then a parser (or something else?) has to map this XML element to the correct JSON one.

2

There are 2 best solutions below

0
On

I find a solution for this problem but not in the originally wanted kind.

The data are received from a stored procedure. This I get as XML and wanted to convert this to the JSON from the question. Because a JSON like this could not be created from a corresponding XML (this would be not well-formed) I don't think that there is a clean solution for these issue. I let the question open just in case there might be a good possibility.

Now I solved it in the following way. I have a stored procedure which delivers the data as JSON now. Because BizTalk works only with XML this JSON is included in an XML structure. Then I wrote a custom pipeline component which extract the JSON from the XML and transmit it out of BizTalk to the destination system. So I get the JSON structure without serializing from XML.

This is more a workaround but something which works good.

0
On

Create an xsd schema that produces something like the following. What i did was put the complex stuff as just the value of a simple element, like the attr and attributes elements. I'm not sure if the JSON Encoder will accept values like this, but i guess it's worth a shot.

The only downside is that your transform will be a little more complex because you will need to produce your JSON partly. But i'm feeling this would be a elegant way to solve it, as you're still making the most out of the stock components.

<element>
    <anotherGuid>05060c4c-f0af-46b8-810e-30c0c00a379e</anotherGuid>
    <attr>
        b587d548-8aa6-42b7-b292-0f3e13452c35": {
            "1": "-2.073420455529934786"
        }
    </attr>
    <attributes>
        "4": "2019-11-05T20:30:57.6Z",
        "8": "6",
        "10": "8",
        "13": "7",
        "27": "3"
    </attributes>
    <guid>80974561-a449-4a94-8b3e-970822b84406</guid>
    <lastModified>2019-11-09T01:44:34.157Z</lastModified>
</element>