how to convert Json array to XSD

2.2k views Asked by At

I am currently mapping Json and XSD. In Json there is array contains several items with same elements.

the Json array is like:

"Item":[  
   {  
      "ItemSequenceNo":0,
      "AQuantity":{  
         "code":"aaa",
         "quantity":1
      },
      "DQuantity":{  
         "code":"ddd",
         "quantity":4
      },
      "Amount":{  
         "currencyID":"USD",
         "value":111
      },

   },
   {  
      "ItemSequenceNo":1,
      "AQuantity":{  
         "code":"aaa",
         "quantity":4
      },
      "DQuantity":{  
         "code":"ddd",
         "quantity":9
      },
      "Amount":{  
         "currencyID":"USD",
         "value":123
      },
   }
]

I tried to map this Json array with XSD:

<xs:element name="Item">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="50">
            <xs:element ref="p:ItemSequenceNo" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="p:AQuantity" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="p:DQuantity" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="p:Amount" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

where the ref is like:

<xs:element name="ItemSequenceNo">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
            <xs:totalDigits value="5"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
<xs:element name="AQuantity">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:integer">
                <xs:attribute name="code">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="0"/>
                            <xs:maxLength value="3"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
<xs:element name="DQuantity">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:integer">
                <xs:attribute name="code">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="0"/>
                            <xs:maxLength value="3"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
<xs:element name="Amount">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:integer">
                <xs:attribute name="CurrencyID">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="0"/>
                            <xs:maxLength value="3"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

The thing is when I am mapping Json array with XSD, the output XML ordered by array item(elements) like below:

<item>
    <ItemSequenceNo >0</ItemSequenceNo>
    <ItemSequenceNo >1</ItemSequenceNo>
    <AQuantity code="aaa">1</AQuantity>
    <AQuantity code="aaa">4</AQuantity>
    <DQuantity code="ddd">4</DQuantity>
    <DQuantity code="ddd">9</DQuantity>
    <Amount CurrencyID="USD">111</Amount>
    <Amount CurrencyID="USD">123</Amount>
</item>

while the result I am expecting is two separate blocks:

<item>
    <ItemSequenceNo >0</ItemSequenceNo> 
    <AQuantity code="aaa">1</AQuantity> 
    <DQuantity code="ddd">4</DQuantity> 
    <Amount CurrencyID="USD">111</Amount>
</item>

<item>
    <ItemSequenceNo >1</ItemSequenceNo>
    <AQuantity code="aaa">4</AQuantity>
    <DQuantity code="ddd">9</DQuantity>
    <Amount CurrencyID="USD">123</Amount>
</item>

Does anyone have any idea on how can I get this? By modifying Json/XSD, or is there any function in MapForce that could achieve this?

1

There are 1 answers

0
Kai Weber On

You already solved it, as you said in your comment, but for people who are coming here with the same problem, looking for a solution, it would be nice to provide an answer here.

It may be a bit counterintuitive, that in MapForce you don't draw the line from the Item-Array in the JSON input to the Item node in the XML output. If you do so, you'll get the unified output that BOSubuntu has described. Instead, you draw the line from the objects that the array contains (see picture below, line marked in red). This would give the desired output.

JSON-Mapping to XML in Altova MapForce