Kotlin – Convert Json(String/File) to/from XML(String/File)

In the tutorial, we show how to convert Json to XML and vice versa in Kotlin language.

Json dependency

For convert Json to/from XML we use JSONObject, so we need a dependency:


  org.json
  json
  20180130

Kotlin – Convert Json to XML

Convert Json String to XML String
var jsonStr = "{\"Customer\": {" +
					    "\"address\": {" +
					        "\"street\": \"NANTERRE CT\"," +
					        "\"postcode\": 77471" +
					    "}," + 
					    "\"name\": \"Mary\"," +
					    "\"age\": 37" +
					"}}"

var json = JSONObject(jsonStr)
var xml = XML.toString(json)

println(xml)
/*
 	
NANTERRE CT77471
Mary37
*/
Convert Json File to XML file
val jsonFile = System.getProperty("user.dir") + "\\file.json"
val xmlFile = System.getProperty("user.dir") + "\\file.xml"

jsonStr = File(jsonFile).readText()
json = JSONObject(jsonStr)

File(xmlFile).writeText(XML.toString(json))
Kotlin – Full Program
package com.javasampleapproach.kotlin.jsonxml

import java.io.File
import org.json.JSONObject
import org.json.XML

fun main(args: Array) {
	// 1. Convert Json String -> XML String
	var jsonStr = "{\"Customer\": {" +
						    "\"address\": {" +
						        "\"street\": \"NANTERRE CT\"," +
						        "\"postcode\": 77471" +
						    "}," + 
						    "\"name\": \"Mary\"," +
						    "\"age\": 37" +
						"}}"
	
	var json = JSONObject(jsonStr)
	var xml = XML.toString(json)
	
	println(xml)
	/*
	 	
NANTERRE CT77471
Mary37
--> pretty-print
NANTERRE CT 77471
Mary 37
*/ // 2. Convert Json File -> XML File val jsonFile = System.getProperty("user.dir") + "\\file.json" val xmlFile = System.getProperty("user.dir") + "\\file.xml" jsonStr = File(jsonFile).readText() json = JSONObject(jsonStr) File(xmlFile).writeText(XML.toString(json)) }

Kotlin – Convert XML to Json

Convert XML String to Json String
val PRETTY_PRINT_INDENT_FACTOR = 4

var xmlString = "" + 
		"  Mary" + 
		"  37" + 
		"  
" + " NANTERRE CT" + " 77471" + "
" + "
" var xmlJSONObj = XML.toJSONObject(xmlString) val jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR) println(jsonPrettyPrintString) /* {"Customer": { "address": { "street": "NANTERRE CT", "postcode": 77471 }, "name": "Mary", "age": 37 }} */
Convert XML File to Json File
var xmlFile = System.getProperty("user.dir") + "\\file.xml"

xmlString = File(xmlFile).readText()
xmlJSONObj = XML.toJSONObject(xmlString)

val jsonFile = System.getProperty("user.dir") + "\\file.json"
File(jsonFile).writeText(xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR))
Kotlin – Full Program
package com.javasampleapproach.kotlin.jsonxml

import java.io.File
import org.json.JSONObject
import org.json.XML

fun main(args: Array) {
	// 1. Convert XML String -> Json String
	val PRETTY_PRINT_INDENT_FACTOR = 4
	
	var xmlString = "" + 
			"  Mary" + 
			"  37" + 
			"  
" + " NANTERRE CT" + " 77471" + "
" + "
" var xmlJSONObj = XML.toJSONObject(xmlString) val jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR) println(jsonPrettyPrintString) /* {"Customer": { "address": { "street": "NANTERRE CT", "postcode": 77471 }, "name": "Mary", "age": 37 }} */ // 2. Convert XML File -> Json File var xmlFile = System.getProperty("user.dir") + "\\file.xml" xmlString = File(xmlFile).readText() xmlJSONObj = XML.toJSONObject(xmlString) val jsonFile = System.getProperty("user.dir") + "\\file.json" File(jsonFile).writeText(xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR)) }
0 0 votes
Article Rating
Subscribe
Notify of
guest
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments