0

I have an XML where the structure looks as below. It is a complex XML where we have multiple attributes. How to convert this XML to JSON using java. Please suggest.

The input looks something like below.

input

<?XML version="1.0" encoding="UTE-8"?>
        <Products>
            <Product>
                <List>
                    <Property name="description" value="Skype"/>
                </List>
            </Product>
        </Products>

Expected Output is

 {
       "Products": {
          "Product": {
             List": [
                {
                   "description": "Skype"
                }
             ]
          }
       }
    } 
Ram
  • 11
  • 1

1 Answers1

0

You can achieve with the JSON Api with something like below

import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;

And then in the code use below way:

JSONObject xmlJSONObj = XML.toJSONObject(XML_TEXT);
        String jsonString = xmlJSONObj.toString();
        System.out.println(jsonString );
mhasan
  • 3,703
  • 1
  • 18
  • 37
  • Hi, Tried your code. Getting the following output. { "Products": { "Product": { List": [ { "name": "description", "value": "Skype" } ] } } } – Ram Jan 04 '17 at 11:01