---
title: "Json"
sidebar_label: "Json"
description: "Kotlin binding reference: Json."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

//[clika-runtime](../../../index.md)/[io.clika.runtime](../index.md)/[Json](index.md)

# Json

[common]\
class [Json](index.md)

A JSON document: null, a boolean, a number, a string, an array or an object. Parse one with [parse](Companion/parse.md) or [readFile](Companion/readFile.md), build one with [newObject](Companion/newObject.md) / [newArray](Companion/newArray.md) and the scalar factories, read it with the `is` predicates, the `as` readers and [at](at.md), write it with [set](set.md) and [add](add.md), and serialize it with [dump](dump.md) or [writeFile](writeFile.md). A read answers an independent copy, a write copies the given value in, so no document ever aliases another. `AutoCloseable`: [close](close.md) releases the document.

```kotlin
Json.newObject().use { request ->
    request.set("model", "m").set("n", 1L)
    val text = request.dump()               // {"model":"m","n":1}
    Json.parse(text).use { back -> back.at("n").use { it.asLong() } }
}
```

## Types

| Name | Summary |
|---|---|
| [Companion](Companion/index.md) | [common]<br>object [Companion](Companion/index.md) |

## Properties

| Name | Summary |
|---|---|
| [isArray](isArray.md) | [common]<br>val [isArray](isArray.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for an array. |
| [isBoolean](isBoolean.md) | [common]<br>val [isBoolean](isBoolean.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for a boolean. |
| [isNull](isNull.md) | [common]<br>val [isNull](isNull.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for the null value. |
| [isNumber](isNumber.md) | [common]<br>val [isNumber](isNumber.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for a number, integer or floating-point. |
| [isObject](isObject.md) | [common]<br>val [isObject](isObject.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for an object. |
| [isString](isString.md) | [common]<br>val [isString](isString.md): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True for a string. |
| [kind](kind.md) | [common]<br>val [kind](kind.md): [JsonKind](../JsonKind/index.md)<br>The value's kind. |
| [size](size.md) | [common]<br>val [size](size.md): [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)<br>The number of members of an object or elements of an array; 0 for a scalar or null. |

## Functions

| Name | Summary |
|---|---|
| [add](add.md) | [common]<br>fun [add](add.md)(value: [Json](index.md)): [Json](index.md)<br>Append a copy of [value](add.md) to an array, turning a null into an array first. Returns this for chaining. |
| [asBoolean](asBoolean.md) | [common]<br>fun [asBoolean](asBoolean.md)(): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>The value as a boolean; another kind fails with `INVALID_ARGUMENT`. |
| [asDouble](asDouble.md) | [common]<br>fun [asDouble](asDouble.md)(): [Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)<br>The value as a double (any number reads through); a non-number fails with `INVALID_ARGUMENT`. |
| [asLong](asLong.md) | [common]<br>fun [asLong](asLong.md)(): [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)<br>The value as a 64-bit integer (any number reads through); a non-number fails with `INVALID_ARGUMENT`. |
| [asString](asString.md) | [common]<br>fun [asString](asString.md)(): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>The value as a string; another kind fails with `INVALID_ARGUMENT`. |
| [at](at.md) | [common]<br>fun [at](at.md)(index: [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)): [Json](index.md)<br>A copy of the array element at [index](at.md); fails when out of range or on a non-array.<br>[common]<br>fun [at](at.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)): [Json](index.md)<br>A copy of the object member at [key](at.md); fails with `NOT_FOUND` when absent or on a non-object. |
| [close](close.md) | [common]<br>open fun [close](close.md)() |
| [contains](contains.md) | [common]<br>fun [contains](contains.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)): [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)<br>True when this is an object holding [key](contains.md). |
| [copy](copy.md) | [common]<br>fun [copy](copy.md)(): [Json](index.md)<br>An independent deep copy of this document. |
| [dump](dump.md) | [common]<br>fun [dump](dump.md)(indent: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) = -1): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>The JSON text: compact on one line by default, or indented by [indent](dump.md) spaces per level when it is 0 or more. |
| [keys](keys.md) | [common]<br>fun [keys](keys.md)(): [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)&gt;<br>The member keys of an object, in insertion order; empty for any other kind. |
| [set](set.md) | [common]<br>fun [set](set.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [Json](index.md)): [Json](index.md)<br>Set the member [key](set.md) to a copy of [value](set.md), turning a null into an object first. Returns this for chaining.<br>[common]<br>fun [set](set.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)): [Json](index.md)<br>Set the member [key](set.md) to a boolean.<br>[common]<br>fun [set](set.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)): [Json](index.md)<br>Set the member [key](set.md) to a floating-point number.<br>[common]<br>fun [set](set.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)): [Json](index.md)<br>Set the member [key](set.md) to an integer.<br>[common]<br>fun [set](set.md)(key: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), value: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)): [Json](index.md)<br>Set the member [key](set.md) to a string. |
| [toString](toString.md) | [common]<br>open override fun [toString](toString.md)(): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) |
| [writeFile](writeFile.md) | [common]<br>fun [writeFile](writeFile.md)(path: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html), indent: [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) = 2)<br>Write the JSON text to [path](writeFile.md) (parent directories are created); [indent](writeFile.md) as [dump](dump.md), 2 by default. |