Reading and Writing JSON in Kotlin with Jackson

Reading and Writing JSON in Kotlin with Jackson

In this article we'll be taking a look at how to read and write JSON files in Kotlin, specifically, using the Jackson library. Jackson Dependency To utilize Jackson, we'll want to add its jackson-module-kotlin dependency to our project. If you're using Maven, you can simply add: <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-kotlin</artifactId> <version>2.12.1</version> </dependency> Or, if you're using Gradle, you can add: implementatio

In this article we'll be taking a look at how to read and write JSON files in Kotlin, specifically, using the Jackson library.

Jackson Dependency

To utilize Jackson, we'll want to add its jackson-module-kotlin dependency to our project. If you're using Maven, you can simply add:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
    <version>2.12.1</version>
</dependency>

Or, if you're using Gradle, you can add:

implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.12.1'

With the dependency in place, let's define a JSON object we'll want to read:

{
   "id":101,
   "username":"admin",
   "password":"Admin123",
   "fullName":"Best Admin"
}

Reading a JSON Object to Kotlin Object

Let's take a look at how we can deserialize a JSON object into a Kotlin object. Since we'll want to convert the JSON contents into a Kotlin object, let's define a User data class:

data class User (
    val id: Int,
    val username: String,
    val password: String,
    val fullName: String
)

Then, we'll want to instantiate the object mapper, which can easily be done with:

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

// Registering the Kotlin module with the ObjectMpper instance
val mapper = jacksonObjectMapper()

// JSON String
val jsonString = """{
    "id":101,
    "username":"admin",
    "password":"Admin123",
    "fullName":"Best Admin"
    }"""

With that done, we can pass JSON contents into our ObjectMapper methods, such as readValue(), as usual for Jackson:

// Read data from a JSON string
val userFromJson = mapper.readValue<User>(jsonString)
// Or
val userFromJsonWithType: User = mapper.readValue(jsonString)

If we print the value of userFromJson we'll see something like this:

User(id=101, username=admin, password=Admin123, fullName=Best Admin)

The readValue() function can be used with or without the Class parameter, as you saw a little earlier with the two variables (userFromJson and userFromJsonWithType).

Note: Using the function without the Class parameter will materialize the type and automatically create a TypeReference for Jackson.

kotlin

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Develop an Android App with Kotlin?

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top Kotlin Android App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top-notch Kotlin App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Kotlin or React Native | React Native vs Kotlin Performance Comparison

This blog provides quick analysis to decide whether to opt for Kotlin or React Native to build robust and secure mobile application.

How to use Kotlin - from the Lead Kotlin Language Designer

In this Kotlin tutorial, the lead Kotlin language designer will show you how you can write more idiomatic Kotlin, what the benefits are, and help you discover some of the most powerful yet lesser known features of Kotlin. Kotlin is similar to the Java programming language, so it's natural that your Kotlin code looks very much like Java code when you are first start to use the language.