In this tutorial, I will show you way to convert XML to JSON & vice versa: convert JSON to XML. You also know how to read XML/JSON file and convert data into JSON/XML file.
Related Posts:
– Kotlin – Convert JSON to Object and vice versa using Jackson
– Kotlin – Convert object to/from JSON string using Gson
org.json Overview
The org.json packages supports JSON encoders/decoders. It also includes features to convert between JSON and XML, HTTP headers, Cookies, and CDL.
Before working with this library, we need to add it to our project.
– Dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
– or External Jar: json-20190722.jar
Then we import org.json.JSONObject and org.json.XML as following code:
import org.json.JSONObject
import org.json.XML
Convert XML to JSON
XML String to JSON
There are 2 steps:
- use
XML.toJSONObject()
function to retrieveJSONObject
from XML string - use
JSONObject.toString()
method to get JSON string fromJSONObject
package com.bezkoder.kotlin.xmljson
import org.json.JSONObject
import org.json.XML
fun main(args: Array<String>) {
val PRETTY_PRINT_INDENT_FACTOR = 2
val xmlStr = """
<Author>
<address>
<street>Internet Broadline</street>
<postcode>123456</postcode>
</address>
<name>bezkoder</name>
<age>26</age>
</Author>
"""
val jsonObj = XML.toJSONObject(xmlStr)
val jsonPrettyPrintString = jsonObj.toString(PRETTY_PRINT_INDENT_FACTOR)
println(jsonPrettyPrintString)
}
Output:
{"Author": {
"address": {
"street": "Internet Broadline",
"postcode": 123456
},
"name": "bezkoder",
"age": 26
}}
XML File to JSON File
With the help of java.io.File
:
- read XML string using
File.readText()
- convert XML string to
JSONObject
usingXML.toJSONObject()
- use
JSONObject.toString()
method to get JSON string fromJSONObject
- write JSON string to JSON file using
File.writeText()
package com.bezkoder.kotlin.xmljson
import java.io.File
import org.json.JSONObject
import org.json.XML
fun main(args: Array<String>) {
val PRETTY_PRINT_INDENT_FACTOR = 2
val xmlFile = System.getProperty("user.dir") + "\\bezkoder.xml"
val xmlStr = File(xmlFile).readText()
val jsonObj = XML.toJSONObject(xmlStr)
val jsonFile = System.getProperty("user.dir") + "\\output.json"
File(jsonFile).writeText(jsonObj.toString(PRETTY_PRINT_INDENT_FACTOR))
}
output.json file:
{"Author": {
"address": {
"street": "Internet Broadline",
"postcode": 123456
},
"name": "bezkoder",
"age": 26
}}
Convert JSON to XML
JSON String to XML
There are 2 steps:
- create
JSONObject
from JSON string usingJSONObject()
constructor method - use
XML.toString()
method to get XML string fromJSONObject
package com.bezkoder.kotlin.xmljson
import org.json.JSONObject
import org.json.XML
fun main(args: Array<String>) {
val jsonStr = """
{
"Author": {
"address": {
"street": "Internet Broadline",
"postcode": 123456
},
"name": "bezkoder",
"age": 26
}
}
"""
val json = JSONObject(jsonStr)
val xml = XML.toString(json)
println(xml)
}
Output:
<Author><address><street>Internet Broadline</street><postcode>123456</postcode></address><name>bezkoder</name><age>26</age></Author>
JSON File to XML File
With the help of java.io.File
:
- read JSON string from file using
File.readText()
- get
JSONObject
from JSON string usingJSONObject()
constructor method - use
XML.toString()
method to get XML string fromJSONObject
- write XML string to XML file using
File.writeText()
package com.bezkoder.kotlin.xmljson
import java.io.File
import org.json.JSONObject
import org.json.XML
fun main(args: Array<String>) {
val jsonFile = System.getProperty("user.dir") + "\\bezkoder.json"
val jsonStr = File(jsonFile).readText()
val jsonObj = JSONObject(jsonStr)
val xmlFile = System.getProperty("user.dir") + "\\output.xml"
File(xmlFile).writeText(XML.toString(jsonObj))
}
output.xml file:
<Author><address><street>Internet Broadline</street><postcode>123456</postcode></address><name>bezkoder</name><age>26</age></Author>
Underscore-java library has a static method U.jsonToXml(json).
You’re a extremely skilled programmer. Thanks for this tutorial.