Working with Json using Java or Kotlin
Simon Scholz2025-07-201 min readComments /Feedback / Requests?

Want to dive deeper?

Book a 1:1 session with me to discuss your challenges and get tailored solutions.

Book a session

Java and Kotlin offer a large variety of tools to work with Json data, e.g., Jackson, Gson or Kotlinx Serialization Json.

Prerequisites

  • Java / Kotlin
  • Jbang or Gradle

Using Jackson´s JsonNode

In case you only need a small fraction of a json document you can utilize the at method instead of having to create a DTO object.

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

val jsonBody =
    """
    {
      "payload": {
        "resource": {
          "id": "12345",
          "typeId": "order"
        }
      }
    }
    """.trimIndent()
val objectMapper = jacksonObjectMapper()
val jsonNode: JsonNode? = objectMapper.readTree(jsonBody)

val resourceId =
    jsonNode?.at("/payload/resource/id")?.let {
        if (it.isMissingNode) {
            null
        } else {
            it.asText()
        }
    }

Sources