XML parameter object conversion to JSON format


Example XML object:

1
    <artikelids type="C">1713</artikelids>

In order to build JSON parameter object you should start from XML tag name and put it as a value of JSON name field:

1
 {"name" : "artikelids" } 

Next step is to convert XML type attribute to proper JSON field according to below example:

1
 { "name" : "artikelids", "type" : "C" } 

Last step is to take the value of XML tag and place it as a value of JSON value field:

1
 { "name" : "artikelids", "type" : "C", "value": "1713" } 

In case of number or boolean value types, the value should be placed without quotation marks:

1
 { "name" : "artikelids", "type" : "L", "value": true } 

or

1
 { "name" : "artikelids", "type" : "N", "value": 444 } 

Nested XML structure conversion


You probably would like to convert nested XML structures to JSON parameter object. Example nested XML structure:
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8" ?>
    <artikelvkpreise type="U">
        <vk1 type="L"> true </vk1>
        <vk2 type="L"> true </vk2>
    </artikelvkpreise>

In order to build JSON parameter object you should start from XML tag name and put it in as a value of JSON name value:

1
 {"name" : "artikelvkpreise" } 

Next step is to convert XML type attribute to proper JSON filed according to below example:

1
{ "name" : "artikelids", "type" : "U" } 

Next step is to take the value of XML tag. Since, value of the artikelvkpreise tag is an array of other XML tags you should convert also those tags according to above rules.

1
 { "name" : "artikelids", "type" : "U", "value":[ ] } 

Conversion of vk1 tag:

1
 { "name" : "vk1", "type" : "L", "value":true }

Conversion of vk2 tag:

1
 { "name" : "vk2", "type" : "L", "value":true } 

Last step is to place converted JSON objects in value array:

1
2
3
4
5
6
    { "name" : "artikelids", "type" : "U", 
      "value": [ 
                { "name" : "vk1", "type" : "L", "value":true },
                { "name" : "vk2", "type" : "L", "value":true }    
            ]
    }