Javatpoint Logo
Javatpoint Logo

Convert XML to JSON in Java

JSON stands for JavaScript object notation, is a lightweight format for storing and transporting the data. It stores the data as the key-value pair. Most of the applications use this format for transmitting the data from the server to the web page, or vice-versa. However, we can use the org.json.XML class in Java to convert the XML to JSON. This class provides a static method XML.toJSONObject to convert XML to JSON Array.

To access the org.json package, we need to add the dependency in our pom.xml file. Add the following code into the pom.xml file of the maven project.

Now, we can access the org.json package and use the methods provided by the org.json.XML class. Consider the following example, in which we convert the XML string into the JSON array.

Here, we have created the Maven project to configure the pom.xml in the project and add the dependencies.

Example:

Output:


{"root": {"test": [
    {
        "attribute": "text1",
        "content": "javatpoint"
    },
    {
        "attribute": "text2",
        "content": "JTP"
    }
]}}





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA