0

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.

r3verse
  • 1,000
  • 8
  • 19
mburm
  • 1,417
  • 2
  • 17
  • 37
  • What exactly is your question? You tagged your question [tag:c#], is your question, *How can I generate a JSON file like the one shown in c#?* Is it, *How can I deserialize a JSON file like the one shown in c#?* Or is it something about converting XML to JSON? – dbc Nov 09 '19 at 06:05
  • Why do you care about XML? Why not just define an appropriate c# model and serialize it to JSON? Since the XML you would need to convert here isn't even well-formed it doesn't seem to be helping. – dbc Nov 09 '19 at 06:36
  • Internally BizTalk only works with XML. There is no other possibility. What I can do is getting the output stream before sending and do something in C# in a Pipeline Component. There i.e. I can deserialize the XML to a C# object and serialize this to JSON. But my XML wouldn't give a valid object because something like `public string 4 { get; set; }` is not allowed. Important is that I get at the end the shown JSON. If the XML looks different but will be serialized to this JSON it's ok. I have no idea how I the serializer has to look like to create this JSON from XML or C# object. – mburm Nov 09 '19 at 06:58
  • Is the "XML" shown in your question the actual XML that BizTalk is writing to the output stream? Because it isn't, as you note, well-formed. If not, can you share an example of the actual XML? Or are you trying to design XML that could be converted to the JSON shown using c#? – dbc Nov 09 '19 at 07:04
  • BizTalk wouldn't handle this because it's not well-formed. Actually I have only the JSON and need a XML from which I can serialize to the JSON. If the solution includes a deserialization to a. NET object first it might be also ok. – mburm Nov 09 '19 at 07:32
  • Your question is still a little unclear. But `JsonReaderWriterFactory` can translate from JSON to XML and back, as long as the XML conforms to the rules laid out in [Mapping Between JSON and XML](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/mapping-between-json-and-xml). Does this meet your needs? Demo: https://dotnetfiddle.net/yBF0j0 – dbc Nov 09 '19 at 23:24
  • 2
    @dbc BizTalk has a few components that make translations from xml to different message formats like flat file and json. The asker is trying to figure out how to still leverage these components. Also see the `JsonEncoder Class` that he's trying to leverage: https://learn.microsoft.com/en-us/dotnet/api/microsoft.biztalk.component.jsonencoder?view=bts-dotnet – r3verse Nov 10 '19 at 14:46

2 Answers2

0

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>

r3verse
  • 1,000
  • 8
  • 19
0

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.

mburm
  • 1,417
  • 2
  • 17
  • 37