Simple Kotlin JSON Libraries Compatible with .main.kts Scripting (No Compiler Plugin Required)
When using Kotlin scripting via .main.kts files, a key limitation is that libraries requiring a compiler plugin (like kotlinx.serialization) are not straightforwardly usable, since .main.kts scripts do not support automatic compiler plugin loading. Therefore, you need JSON libraries that work out-of-the-box with just a dependency declaration.
Below are simple, widely-used JSON libraries that do not require a compiler plugin and are compatible with .main.kts scripting:
1. Moshi
Description: Moshi is a modern, minimalistic JSON library for Kotlin and Java. It supports reflection-based adapters for Kotlin data classes, which means you do not need to use code generation or a compiler plugin—just add the dependency and use it.
How to use in .main.kts:
kotlin
@file:DependsOn("com.squareup.moshi:moshi:1.15.2")
@file:DependsOn("com.squareup.moshi:moshi-kotlin:1.15.2")
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
data class Person(val name: String, val age: Int)
val moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
val adapter = moshi.adapter(Person::class.java)
val json = """{"name":"Alice","age":30}"""
val person = adapter.fromJson(json)
println(person)
Notes: The reflection-based adapter (moshi-kotlin) adds a dependency on kotlin-reflect, but this is just a regular library, not a compiler plugin[2][7].
2. Gson
Description: Gson is a classic Java JSON library that works seamlessly in Kotlin. It does not require any Kotlin-specific plugins or code generation.
How to use in .main.kts:
kotlin
@file:DependsOn("com.google.code.gson:gson:2.11.0")
import com.google.gson.Gson
data class Person(val name: String, val age: Int)
val gson = Gson()
val json = """{"name":"Bob","age":25}"""
val person = gson.fromJson(json, Person::class.java)
println(person)
Notes: Gson uses Java reflection and works with Kotlin data classes, though it does not understand Kotlin-specific features like default parameters or non-nullability as well as Moshi does[6].
3. Essential KSON
Description: Essential KSON is a minimalistic, multiplatform JSON parser inspired by Gson. It is designed to be lightweight and does not require a compiler plugin.
How to use in .main.kts:
kotlin
@file:DependsOn("io.github.essential-dis/essential-kson:2.5.0")
import kson.Kson
val json = """{"foo": 42, "bar": "baz"}"""
val obj = Kson.parse(json)
println(obj)
Notes: This library is newer and less widely adopted, but it is specifically designed for multiplatform and scripting scenarios[4].
4. org.json / JSONObject (JVM only)
Description: The org.json package is included in many JVM environments and provides a basic JSONObject class for parsing and generating JSON.
How to use in .main.kts:
kotlin
@file:DependsOn("org.json:json:20240303")
import org.json.JSONObject
val json = """{"foo": 123, "bar": "baz"}"""
val obj = JSONObject(json)
println(obj.getInt("foo"))
Notes: This is a very basic, manual approach and does not provide automatic mapping to data classes[1][9].
Not Recommended for .main.kts Scripting
kotlinx.serialization: Requires a compiler plugin, which is not supported in .main.kts scripts without complex manual setup[3][5][6].
Jackson: While usable, it is heavier and more Java-centric. For scripting, Moshi or Gson are generally simpler[8].
Summary Table
Library
Compiler Plugin Needed
Data Class Support
Scripting-Friendly
Notes
Moshi
No
Yes (with reflect)
Yes
Needs moshi-kotlin
Gson
No
Yes (basic)
Yes
No Kotlin-specific features
Essential KSON
No
No (manual)
Yes
Minimal, multiplatform
org.json/JSONObject
No
No (manual)
Yes
JVM only, manual parsing
kotlinx.serialization
Yes
Yes
No
Needs compiler plugin
Conclusion
For simple and idiomatic JSON handling in Kotlin .main.kts scripts, Moshi (with moshi-kotlin) and Gson are the best choices. Both require only a @file:DependsOn declaration and work without any compiler plugin support. For minimal or multiplatform needs, consider Essential KSON. Avoid kotlinx.serialization in .main.kts unless you are prepared for complex manual setup[2][4][6][7].