---
title: "JsonError"
url: https://una-lang.org/en/docs/std/prelude/JsonError/
release: v0.4.0
edition: "0.2 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/listing.json
---

# JsonError

A kind of the prelude, in scope in every module. Edition 0.2 and later.

```una
pub kind JsonError = Syntax(at: Int) | TooDeep(at: Int) | DuplicateKey(at: Int) | NumberTooLong(at: Int)
```

Why a text is not one JSON value, at the first error in reading order (`spec/23 §3`).

## Variants

- `Syntax(at)`: the text is not one JSON value; `at` is the scalar position\, from 0\, of the first scalar value where no JSON text can continue\, or the length of the text when it ends early
- `TooDeep(at)`: arrays and objects nest more than 512 deep; `at` is the scalar position\, from 0\, of the bracket that opens the 513th level
- `DuplicateKey(at)`: an object has two equal keys\, compared after unescaping; `at` is the scalar position\, from 0\, of the second of two equal keys
- `NumberTooLong(at)`: an integer has more than 4300 digits; `at` is the scalar position\, from 0\, of the integer\'s first character

## See also

[`parse_json`](https://una-lang.org/en/docs/std/prelude/parse_json/)

## Example

`src/shop.una`

```una
module shop

--- An order read from JSON, and why a text is not one JSON value.
order(raw: Text) -> Text
=
  let outcome = attempt parse_json(raw)
  when outcome
    is Ok(value) -> written(value)
    is Err(failure) -> reason(failure)

written(value: Json) -> Text
=
  let text = value.to_json()
  "read {text}"

reason(failure: JsonError) -> Text
=
  when failure
    is Syntax(at) -> "not JSON at {at}"
    is TooDeep(at) -> "nested too deep at {at}"
    is DuplicateKey(at) -> "a key twice at {at}"
    is NumberTooLong(at) -> "a number too long at {at}"

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(order("\{\"lamp\": 3, \"desk\": 1\}"))
  @console.print(order("[1, 2,]"))
  @console.print(order("\{\"lamp\": 3, \"lamp\": 4\}"))
```

`una run .` prints:

```text
read {"desk":1,"lamp":3}
not JSON at 6
a key twice at 12
```

## Specification

- [`spec/23 §3`](https://una-lang.org/en/docs/spec/23-json/#s3)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
