Kotlin – Convert XML to JSON & JSON to XML

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:

  1. use XML.toJSONObject() function to retrieve JSONObject from XML string
  2. use JSONObject.toString() method to get JSON string from JSONObject
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:

  1. read XML string using File.readText()
  2. convert XML string to JSONObject using XML.toJSONObject()
  3. use JSONObject.toString() method to get JSON string from JSONObject
  4. 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:

  1. create JSONObject from JSON string using JSONObject() constructor method
  2. use XML.toString() method to get XML string from JSONObject
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:

  1. read JSON string from file using File.readText()
  2. get JSONObject from JSON string using JSONObject() constructor method
  3. use XML.toString() method to get XML string from JSONObject
  4. 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>

kotlin-xml-to-json-output-xml-file

Further Reading

  1. https://mvnrepository.com/artifact/org.json/json
  2. JSONObject Reference
  3. org.json.XML API

2 thoughts to “Kotlin – Convert XML to JSON & JSON to XML”

Comments are closed to reduce spam. If you have any question, please send me an email.