Hi Susinda,

I found this library json-lib [1] and I have tried few XML to JSON
conversions. It seems like this library preserves the order. But there are
few problems that comes with it. First thing is, it has 5 other library
dependencies[1]. Also, the output JSON of this particular library is
slightly different from the one we are currently using. Therefor if we are
gonna use this we will have to modify the post processing part as well.

Apache Camel uses json-lib library to convert between XML and JSON. [2]

I have included one of the samples that I have tried.
[3] Input XML file
[4] XML to JSON converted output using json-lib library
[5] XML to JSON converted output using org.json library

[1]. http://json-lib.sourceforge.net/
[2]. http://camel.apache.org/xmljson.html


[3].
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";
xmlns:urn="urn:enterprise.soap.sforce.com" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";>
   <soapenv:Header>xxx</soapenv:Header>
   <soapenv:Body>
      <h:person xmlns:h="http://www.w3.org/TR/html4/";>
         <firstName>sample</firstName>
         <phone primary="false">
            0112655655
            <ext>01</ext>
         </phone>
         <phone primary="false">
            0112655655
            <ext>01</ext>
         </phone>
         <email>
            [email protected]
            <ext>wso2.com</ext>
         </email>
      </h:person>
      <cities>
         <city xsi:type="a">colombo</city>
         <city xsi:type="a">kandy</city>
         <city xsi:type="b">galle</city>
      </cities>
      <company>
         WSO2
         <location>CMB</location>
      </company>
      <address>
         <street xsi:type="d">Main Street</street>
         <city>Kandy</city>
      </address>
   </soapenv:Body>
</soapenv:Envelope>

[4].
{
   "soapenv:Envelope":{
      "@xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/";,
      "@xmlns:urn":"urn:enterprise.soap.sforce.com",
      "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
      "soapenv:Header":"xxx",
      "soapenv:Body":{
         "h:person":{
            "@xmlns:h":"http://www.w3.org/TR/html4/";,
            "firstName":"sample",
            "phone":[
               {
                  "@primary":"false",
                  "#text":"0112655655",
                  "ext":"01"
               },
               {
                  "@primary":"false",
                  "#text":"0112655655",
                  "ext":"01"
               }
            ],
            "email":{
               "#text":"[email protected]",
               "ext":"wso2.com"
            }
         },
         "cities":[
            {
               "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
               "@xsi:type":"a",
               "#text":"colombo"
            },
            {
               "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
               "@xsi:type":"a",
               "#text":"kandy"
            },
            {
               "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
               "@xsi:type":"b",
               "#text":"galle"
            }
         ],
         "company":{
            "#text":"WSO2",
            "location":"CMB"
         },
         "address":{
            "street":{
               "@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
               "@xsi:type":"d",
               "#text":"Main Street"
            },
            "city":"Kandy"
         }
      }
   }
}

[5]
{
   "soapenv:Envelope":{
      "soapenv:Body":{
         "h:person":{
            "firstName":"sample",
            "phone":[
               {
                  "ext":1,
                  "content":19618733,
                  "primary":false
               },
               {
                  "ext":1,
                  "content":19618733,
                  "primary":false
               }
            ],
            "xmlns:h":"http://www.w3.org/TR/html4/";,
            "email":{
               "ext":"wso2.com",
               "content":"[email protected]"
            }
         },
         "address":{
            "city":"Kandy",
            "street":{
               "xsi:type":"d",
               "content":"Main Street"
            }
         },
         "cities":{
            "city":[
               {
                  "xsi:type":"a",
                  "content":"colombo"
               },
               {
                  "xsi:type":"a",
                  "content":"kandy"
               },
               {
                  "xsi:type":"b",
                  "content":"galle"
               }
            ]
         },
         "company":{
            "location":"CMB",
            "content":"WSO2"
         }
      },
      "xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/";,
      "xmlns:urn":"urn:enterprise.soap.sforce.com",
      "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance";,
      "soapenv:Header":"xxx"
   }
}


Thanks,

On Thu, Apr 7, 2016 at 11:58 AM, Susinda Perera <[email protected]> wrote:

> Added a PR[1] to their project. If authors merge it we can use the updated
> code without forking.
> [1] - https://github.com/stleary/JSON-java/pull/216
>
> On Wed, Apr 6, 2016 at 7:08 PM, Susinda Perera <[email protected]> wrote:
>
>> Hi All
>>
>> I was able to solve the issue by using option-1 i have mentioned above.
>> Not sure whether its a good practise to fork that code and maintained by
>> us. Any thoughts? can we go ahead with this approach?
>>
>> Thanks
>> Susinda
>>
>> On Wed, Apr 6, 2016 at 8:48 AM, Susinda Perera <[email protected]> wrote:
>>
>>> Hi All
>>>
>>> When an xml instance is given, the created json schema does not maintain
>>> the order (which is in the xml). Reason is for this behavior is when
>>> creating json schema we first convert xml to json and then iterate the json
>>> object to create the json schema. The xml->json conversion process does not
>>> guarantee the order (As json object is an unordered set of name/value
>>> pairs).
>>> I can think of following alternative to overcome this issue.
>>> 1. Fork the org.json source and use a LinkedHashMap for json object
>>> instead of HashMap [1]
>>> 2. Directly generate json schema from xml
>>> 3. Once the xml-> json conversion again iterate the xml and reorder the
>>> json object
>>>
>>> Option 1 - Has the disadvantage of maintaining org.json* code
>>> Option 2 - Have to write from the scratch and have to handle arrays in
>>> xml
>>> Option 3 - Extra processing overhead.
>>>
>>> If you have any ideas please comment.
>>>
>>> [1] -
>>> http://stackoverflow.com/questions/26034370/inverted-order-of-json-elements-in-java-after-xml-conversion
>>>
>>>
>>>
>>> Thanks
>>> Susinda
>>>
>>> --
>>> *Susinda Perera*
>>> Software Engineer
>>> B.Sc.(Eng), M.Sc(Computer Science), AMIE(SL)
>>> Mobile:(+94)716049075
>>> Blog: susinda.blogspot.com
>>> WSO2 Inc. http://wso2.com/
>>> Tel : 94 11 214 5345 Fax :94 11 2145300
>>>
>>>
>>
>>
>> --
>> *Susinda Perera*
>> Software Engineer
>> B.Sc.(Eng), M.Sc(Computer Science), AMIE(SL)
>> Mobile:(+94)716049075
>> Blog: susinda.blogspot.com
>> WSO2 Inc. http://wso2.com/
>> Tel : 94 11 214 5345 Fax :94 11 2145300
>>
>>
>
>
> --
> *Susinda Perera*
> Software Engineer
> B.Sc.(Eng), M.Sc(Computer Science), AMIE(SL)
> Mobile:(+94)716049075
> Blog: susinda.blogspot.com
> WSO2 Inc. http://wso2.com/
> Tel : 94 11 214 5345 Fax :94 11 2145300
>
>
> _______________________________________________
> Architecture mailing list
> [email protected]
> https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture
>
>


-- 
*Eranda Rajapakshe*
Software Engineer
WSO2 Inc.
Mobile : +94784822608
_______________________________________________
Architecture mailing list
[email protected]
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture

Reply via email to