-1

I want to convert an xml to a JSON. Example: convert the below xml to the json given.

<header>
<students>
<info>
    <name>student1</name>
    <class>2</class>
</info>
<info>
<name>student2</name>
<class>3</class>
</info>
</students>
</header>

JSON:

{
"header": {
    "students": [
        {
            "name": "student1",
            "class": "2"
        },
        {
            "name": "student2",
            "class": "3"
        }
    ]
}
}

The question is, how do i convert the element into the array?

Susanna M
  • 93
  • 3
  • 13
  • You can follow this https://www.novixys.com/blog/convert-xml-json/ . – jack jay Sep 06 '18 at 06:15
  • you can see the following link https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java – shohan Sep 06 '18 at 06:58
  • Use an XML library to read the XML, and a JSON library to write the JSON. Though there are a bunch of tools out there who promise JSON/XML conversion, they will do so using their own conventions. If you have your own goal about what should be the resulting JSON given the input XML, then their conventions won't work for you, and you just need to resolve both formats yourself. – kumesana Sep 06 '18 at 08:04
  • can someone give a solution using xsl. I was trying to use the one in this link: https://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/ . it works fine. except that its able to detect the xml array only when there are multiple elements within the array. If there is only a single element in the array, when converted to json, its not considered as array – Susanna M Sep 06 '18 at 09:40
  • Let me know if my [answer](https://stackoverflow.com/q/52197561/1426227) works for you. – cassiomolin Sep 06 '18 at 13:03

4 Answers4

0

You can use org.json library to convert xml string to json object.

Sample program:

import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.json.me.util.XML;

public class XmlToJson {

    public static void main(String args[]) throws JSONException
    {

        String sampleXML = "<COLLEGE><STUDENT><NAME>MACK</NAME><MARKS>90</MARKS></STUDENT><STUDENT><NAME>JACK</NAME><MARKS>82</MARKS></STUDENT></COLLEGE>";
        JSONObject jsonObject = new JSONObject(XML.toJSONObject(sampleXML).toString());
        System.out.println("XML: "+sampleXML);
        System.out.println("JSON: "+jsonObject);
    }

}

Sample Output:

XML: <COLLEGE>
        <STUDENT>
           <NAME>MACK</NAME>
           <MARKS>90</MARKS>
        </STUDENT>
        <STUDENT>
           <NAME>JACK</NAME>
           <MARKS>82</MARKS>
        </STUDENT> 
     </COLLEGE>


JSON: {"COLLEGE": {
       "STUDENT": [
      {
        "MARKS": "90",
        "NAME": "MACK"
      },
      {
        "MARKS": "82",
        "NAME": "JACK"
      }
    ]
  }
}
Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
0

I did the transformation using the xslt provided in the link : https://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/

The issue i was facing was that, this xslt converts xml array to json array only when there are multiple xml elements of the same name. If there is only a single xml element in the array, then it does not convert it to JSON array. I solved this by editing the xsl using my own condition where i check for the name of the array element i want and check if the count is zero for the same and then add '[' and ']' for that condition.

Susanna M
  • 93
  • 3
  • 13
0

Add the following dependency to your application:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

It provides a pretty straighforward way to convert an XML document into a JSON document:

String xml = "<header>\n" +
             "  <students>\n" +
             "    <info>\n" +
             "      <name>student1</name>\n" +
             "      <class>2</class>\n" +
             "    </info>\n" +
             "    <info>\n" +
             "      <name>student2</name>\n" +
             "      <class>3</class>\n" +
             "    </info>\n" +
             "   </students>\n" +
             "</header>";

JSONObject json = XML.toJSONObject(xml);
System.out.println(json);

The output will be:

{"header":{"students":{"info":[{"name":"student1","class":2},{"name":"student2","class":3}]}}}

Then you can perform some manipulation:

JSONArray info = json.getJSONObject("header").getJSONObject("students").getJSONArray("info");
json.getJSONObject("header").put("students", info);
System.out.println(json);

And you'll have the desired output:

{"header":{"students":[{"name":"student1","class":2},{"name":"student2","class":3}]}}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
-2

i tried to create a jason from the xml string you have added. used c# and was able to get the Jason file. You should add the nuget package Newtonsoft.Json to your project.

 public void xmlToJason()
    {
        string xml = @"<header>
                        <students>
                        <info>
                            <name>student1</name>
                            <class>2</class>
                        </info>
                        <info>
                        <name>student2</name>
                        <class>3</class>
                        </info>
                        </students>
                        </header>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        string json = JsonConvert.SerializeXmlNode(doc);

        Console.WriteLine(json);
    }