# Advanced JSON parsing techniques using Moshi and Kotlin

DevFeed: [Advanced JSON parsing techniques using Moshi and Kotlin](<https://devfeed.tech/articles/advanced-json-parsing-techniques-using-moshi-and-kotlin-25877.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/advanced-json-parsing-techniques-using-moshi-and-kotlin-daf56a7b963d?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2018-07-30T23:12:56Z

Content type: tutorial

Language: en

Sources: [Stories by Christophe Beyls on Medium](<https://devfeed.tech/sources/stories-by-christophe-beyls-on-medium.md>)

Topics: [JSON](<https://devfeed.tech/topics/json.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Parser](<https://devfeed.tech/topics/parser.md>), [Android](<https://devfeed.tech/topics/android.md>), [Java](<https://devfeed.tech/topics/java.md>), [Library](<https://devfeed.tech/topics/library.md>), [Streaming](<https://devfeed.tech/topics/streaming.md>)

Tags: [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [code](<https://devfeed.tech/tags/code.md>), [gson](<https://devfeed.tech/tags/gson.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [performance](<https://devfeed.tech/tags/performance.md>), [streaming](<https://devfeed.tech/tags/streaming.md>)

## AI overview

A tutorial on parsing JSON with Moshi and Kotlin. It demonstrates manual streaming parsing, handling required and optional properties, skipping unknown values, and optimizing repeated key comparisons using Moshi and Okio.

## Source excerpt

A match made in parser heaven Moshi is a modern JSON library for Android and Java from Square. It can be considered as the successor to GSON, with a simpler and leaner API and an architecture enabling better performance through the use of the Okio library. It's also the most Kotlin-friendly library you can use to parse JSON files, as it comes with Kotlin-aware extensions. In this article I'm going to demonstrate how to take advantage of features from both the Moshi library and the Kotlin language itself in order to write efficient and robust JSON parsers. The example model and JSON file Consider the following model representing a person: class Person(val id: Long, val name: String, val age: Int = -1) id and name are mandatory properties, while the age is optional with a default value of -1. Our objective is to load a list of Person objects in our application from a JSON file or stream with the following contents: [ { "id": 1, "name": "John", "age": 38 }, { "id": 8, "name": "Lisa", "age": 23 }, { "id": 23, "name": "Karen" } ] In this simple example, the JSON object key names exactly match the Person property names and the "age" key is also optional. 1. Fully manual parsing The most basic way of parsing JSON using Moshi is to use the streaming API, which is similar to the streaming API of GSON and the one provided by the Android Framework. This gives you the most control over the parsing process, which is especially useful when the JSON source is dirty. Typical code would look like this: class ManualParser { fun parse(reader: JsonReader): List<Person> { val result = mutableListOf<Person>() reader.beginArray() while (reader.hasNext()) { var id: Long = -1L var name: String = "" var age: Int = -1 reader.beginObject() while (reader.hasNext()) { when (reader.nextName()) { "id" -> id = reader.nextLong() "name" -> name = reader.nextString() "age" -> age = reader.nextInt() else -> reader.skipValue() } } reader.endObject() if (id == -1L || name == "") { throw JsonDataException