Convert XML to JSON in Kotlin
In this quick article, we’ll see how to convert XML to JSON in Kotlin.
Kotlin didn’t provide any capability to convert XML to JSON. However, you can use the JSON in Java library for quick conversion between JSON and XML.
The idea is to use the static XML.toJSONObject() method from the org.json package to convert the XML to JSON Object. To get the string representation of the JSON object, you can call the toString() method on it. Following is a simple example demonstrating usage of this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import org.json.JSONException import org.json.XML var INDENTATION = 4 var XML_STRING = """ <?xml version="1.0"?> <catalog> <book id="kotlin1"> <author>Bruce Eckel</author> <title>Atomic Kotlin</title> <genre>Computer</genre> <pages>636</pages> <published>November 2021</published> </book> <book id="kotlin2"> <author>Dmitry Jemerov and Svetlana Isakova</author> <title>Kotlin in Action</title> <genre>Computer</genre> <pages>532</pages> <published>3 February 2017</published> </book> <book id="kotlin3"> <author>Dawn Griffiths, David Griffiths</author> <title>Head First Kotlin</title> <genre>Computer</genre> <pages>814</pages> <published>13 February 2019</published> </book> <book id="kotlin4"> <author>Josh Skeen, David Greenhalgh</author> <title>Kotlin Programming: The Big Nerd Ranch Guide</title> <genre>Computer</genre> <pages>532</pages> <published>2018</published> </book> <book id="kotlin5"> <author>Venkat Subramaniam</author> <title>Programming Kotlin</title> <genre>Computer</genre> <pages>462</pages> <published>20 September 2019</published> </book> <book id="kotlin6"> <author>Pierre Yves Saumont</author> <title>The Joy of Kotlin</title> <genre>Computer</genre> <pages>674</pages> <published>21 April 2019</published> </book> </catalog> """.trimIndent() fun main() { try { val jsonObj = XML.toJSONObject(XML_STRING) val json = jsonObj.toString(INDENTATION) println(json) } catch (ex: JSONException) { ex.printStackTrace() } } |
Output:
{"catalog": {"book": [
{
"author": "Bruce Eckel",
"genre": "Computer",
"id": "kotlin1",
"pages": 636,
"published": "November 2021",
"title": "Atomic Kotlin"
},
{
"author": "Dmitry Jemerov and Svetlana Isakova",
"genre": "Computer",
"id": "kotlin2",
"pages": 532,
"published": "3 February 2017",
"title": "Kotlin in Action"
},
{
"author": "Dawn Griffiths, David Griffiths",
"genre": "Computer",
"id": "kotlin3",
"pages": 814,
"published": "13 February 2019",
"title": "Head First Kotlin"
},
{
"author": "Josh Skeen, David Greenhalgh",
"genre": "Computer",
"id": "kotlin4",
"pages": 532,
"published": 2018,
"title": "Kotlin Programming: The Big Nerd Ranch Guide"
},
{
"author": "Venkat Subramaniam",
"genre": "Computer",
"id": "kotlin5",
"pages": 462,
"published": "20 September 2019",
"title": "Programming Kotlin"
},
{
"author": "Pierre Yves Saumont",
"genre": "Computer",
"id": "kotlin6",
"pages": 674,
"published": "21 April 2019",
"title": "The Joy of Kotlin"
}
]}}
That’s all about converting XML to JSON in Kotlin.
Also See: Convert XML to JSON online
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)