There is no question that the future of parsing in Kotlin will be with kotlinx.serialization. It is part of Kotlin libraries. Version kotlinx.serialization 1.0 is finally released

https://github.com/Kotlin/kotlinx.serialization

Copyimport kotlinx.serialization.*
import kotlinx.serialization.json.Json

@Serializable
data class MyModel(val a: Int, @Optional val b: String = "42")

fun main(args: Array<String>) {

    // serializing objects
    val jsonData = Json.encodeToString(MyModel.serializer(), MyModel(42))
    println(jsonData) // {"a": 42, "b": "42"}
    
    // serializing lists
    val jsonList = Json.encodeToString(MyModel.serializer().list, listOf(MyModel(42)))
    println(jsonList) // [{"a": 42, "b": "42"}]

    // parsing data back
    val obj = Json.decodeFromString(MyModel.serializer(), """{"a":42}""")
    println(obj) // MyModel(a=42, b="42")
}
Answer from Elisha Sterngold on Stack Overflow
🌐
BezKoder
bezkoder.com › home › kotlin – convert object to/from json string using gson
Kotlin - Convert object to/from JSON string using Gson - BezKoder
April 11, 2020 - In this tutorial, we’re gonna look at way to parse JSON into Data Class, Array, Map and do the opposite: convert object, Array, Map to JSON in Kotlin with the help of Gson library.
🌐
Kotlin
kotlinlang.org › docs › serialization.html
Serialization | Kotlin Documentation
In turn, deserialization is the ... and converting it into a runtime object. Together, they are essential to most applications that exchange data with third parties. Some data serialization formats, such as JSON and protocol buffers are particularly common. Being language-neutral and platform-neutral, they enable data exchange between systems written in any modern language. In Kotlin, data ...
🌐
Transform
transform.tools › json-to-kotlin
JSON to Kotlin - Transform
An online playground to convert JSON to Kotlin
🌐
BezKoder
bezkoder.com › home › kotlin – convert json to object and vice versa using jackson
Kotlin – Convert JSON to Object and vice versa using Jackson - BezKoder
December 13, 2020 - === JSON to Kotlin Object === 1- ... age=28, messages=[Great!, Im zKoder]) ... convert Object to JSON String using writeValueAsString() function (with or without writerWithDefaultPrettyPrinter() to apply Pretty Prin...
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-json › kotlinx.serialization.json › -json-object
JsonObject | kotlinx.serialization – Kotlin Programming Language
kotlinx-serialization-json/kotlinx.serialization.json/JsonObject · @Serializable(with = JsonObjectSerializer::class · )class JsonObject(content: Map<String, JsonElement>) : JsonElement, Map<String, JsonElement> (source) Class representing JSON object, consisting of name-value pairs, where value is arbitrary JsonElement · Since this class also implements Map interface, you can use traditional methods like Map.get or Map.getValue to obtain Json elements.
Find elsewhere
🌐
ZetCode
zetcode.com › kotlin › json
Kotlin JSON - JSON serialization and deserialization in Kotlin
January 29, 2024 - The kotlinx.serialization is a Kotlin library for serialization. The toJson method serializes the specified object into its equivalent JSON representation.
🌐
Medium
medium.com › @midoripig1009 › working-with-json-in-kotlin-parsing-and-serialization-a62300ec43b8
Working with JSON in Kotlin: Parsing and Serialization | by CuriousCat | Medium
October 28, 2023 - We take an instance of a class and serialize it to a JSON string. ... import kotlinx.serialization.* import kotlinx.serialization.json.* @Serializable data class User(val name: String, val age: Int) fun main() { val user = User("John", 30) val json = Json.encodeToString(user) println(json) // {"name":"John","age":30} } In the example above, we first create a User object.
🌐
DhiWise
dhiwise.com › post › how-to-streamline-data-processing-with-kotlin-json
Mastering Kotlin JSON Serialization for Efficient Data ...
July 10, 2024 - To encode a Kotlin object to a JSON string, the encodeToString function comes into play, converting the object to a JSON representation.
🌐
Scaler
scaler.com › home › topics › converting kotlin data class from json using gson
Converting Kotlin Data Class from JSON using GSON - Scaler Topics
November 6, 2023 - Gson is a Java/Kotlin library for converting Java/Kotlin Objects into JSON representation, also a JSON string to an equivalent Java/Kotlin object.
🌐
Kotlination
kotlination.com › kotlin-convert-object-to-from-json-jackson-2
Convert Object to/from JSON with Jackson 2.x - Kotlination
In object oriented programming, ... a class to have only one instance. In Java, we usually implement using the Singleton Design Pattern: define a class with a private constructor and a static field holding the only existing instance… Read More More Posts » · basic bufferedreader bufferedwriter collections companion objects comparable comparator delegate design pattern file fold function gson inputstream jackson java equivalent json kotlin helloworld ...
🌐
Android Developers
developer.android.com › api reference › jsonobject
JSONObject | API reference | Android Developers
February 26, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-in-kotlin-with-jackson
Reading and Writing JSON in Kotlin with Jackson
February 27, 2023 - In this tutorial, we'll go over how to serialize and deserialize JSON objects and arrays into Kotlin objects and lists and vice versa, using Jackson.
🌐
Json2kt
json2kt.com
JSON to Kotlin Class .kt Data Class Generate Online (POJO Class)
This online kotlin data class generator with dark theme will generate data class with GSON mapping. This will make nice single line format for each field to maintain your code more easily. Input JSON in the box, then you will see the result in the below box in realtime. JSON Tips: JSON key(Parameter) name should be camelCase or snack_case, Don't use same root object...
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-json › kotlinx.serialization.json › -json
Json | kotlinx.serialization – Kotlin Programming Language
Json instance can be configured in its Json {} factory function using JsonBuilder. For demonstration purposes or trivial usages, Json companion can be used instead. Then constructed instance can be used either as regular SerialFormat or StringFormat ...
🌐
Stack Overflow
stackoverflow.com › questions › 75448485 › how-to-convert-json-object-with-objects-to-json-array-with-objects
kotlin - How to convert json object with objects to json array with objects - Stack Overflow
To convert the given JSON object to a list of Notification objects, you can iterate over the key-value pairs in the "Items" object and create a Notification object for each non-null value. Here's some sample Kotlin code that demonstrates this:
🌐
DhiWise
dhiwise.com › post › kotlin-object-to-json-transformation-efficient-data-conversion
Mastering Kotlin Object to JSON Conversion
September 5, 2024 - To serialize a Kotlin object to a JSON string, developers can leverage the kotlinx.serialization library’s serialization functionality. By defining serialization strategies for data classes and invoking the corresponding serialization methods, Kotlin objects can be efficiently converted into JSON strings for transmission or storage.
Top answer
1 of 3
31

In 2019 no-one is really parsing JSON manually. It's much easier to use Gson library. It takes as an input your object and spits out JSON string and vice-versa.

Example:

data class MyClass(@SerializedName("s1") val s1: Int)

val myClass: MyClass = Gson().fromJson(data, MyClass::class.java)
val outputJson: String = Gson().toJson(myClass)

This way you're not working with JSON string directly but rather with Kotlin object which is type-safe and more convenient. Look at the docs. It's pretty big and easy to understand

Here is some tutorials:

  • https://www.youtube.com/watch?v=f-kcvxYZrB4
  • http://www.studytrails.com/java/json/java-google-json-introduction/
  • https://www.tutorialspoint.com/gson/index.htm

UPDATE: If you really want to use JSONObject then use its constructor with a string parameter which parses your JSON string automatically.

val jsonObject = JSONObject(data)
2 of 3
11

Best way is using kotlinx.serialization. turn a Kotlin object into its JSON representation and back marking its class with the @Serializable annotation, and using the provided encodeToString and decodeFromString<T> extension functions on the Json object:

import kotlinx.serialization.*
import kotlinx.serialization.json.*
​
@Serializable
data class User(val name: String, val yearOfBirth: Int)
​
   // Serialization (Kotlin object to JSON string)
​
   val data = User("Louis", 1901)
   val string = Json.encodeToString(data)
   println(string) // {"name":"Louis","yearOfBirth":1901}
​
   // Deserialization (JSON string to Kotlin object)
​
   val obj = Json.decodeFromString<User>(string)
   println(obj) // User(name=Louis, yearOfBirth=1901)

Further examples: https://blog.jetbrains.com/kotlin/2020/10/kotlinx-serialization-1-0-released/