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

# Json

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

```una
pub kind Json = Null | Boolean(value: Bool) | Number(value: Int) | Decimal(text: JsonNumber) | Str(value: Text) | Array(items: List[Json]) | Object(fields: Map[Text, Json])
```

A JSON value (`spec/23 §1`). `Number` is an exact integer, a JSON number with neither a fraction nor an exponent; other numbers are `Decimal`, as written.

## Variants

- `Null`: JSON `null`
- `Boolean(value)`: a JSON boolean; `value` is `true` or `false`
- `Number(value)`: a JSON number with neither a fraction nor an exponent; `value` is the integer\, exact at any size
- `Decimal(text)`: any other JSON number\, so that `Number` and `Decimal` never hold the same number\; two are equal when their texts are\, so `1.0` and `1.00` differ; `text` is the number as written in the input\, a `JsonNumber`
- `Str(value)`: a JSON string\; the positions of `Json` are unlabeled\, so a labeled text given to `Str`\, or a labeled value to any variant\, is U4001\, and a labeled text reaches JSON through a policy\, `quote_json` or interpolation \(`spec/23 §5`\); `value` is the string\, unescaped
- `Array(items)`: a JSON array; `items` is its values\, in order
- `Object(fields)`: a JSON object\, a map ordered by its keys\, so the order of the input is not kept; `fields` is its entries\, by key

## See also

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

## Example

`src/shop.una`

```una
module shop

--- What each value of an item's JSON is.
kind_of(value: Json) -> Text
=
  when value
    is Null -> "nothing"
    is Boolean(flag) -> "yes or no: {flag}"
    is Number(n) -> "a whole number: {n}"
    is Decimal(text) -> "a decimal: {text}"
    is Str(name) -> "a text: {name}"
    is Array(items) -> "a list of {items.len}"
    is Object(fields) -> "an object of {fields.len}"

price() -> Json
=
  parse_json("9.95")
    if error use Null

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(kind_of(Null))
  @console.print(kind_of(Boolean(true)))
  @console.print(kind_of(Number(12)))
  @console.print(kind_of(price()))
  @console.print(kind_of(Str("lamp")))
  @console.print(kind_of(Array([Number(1), Number(2)])))
  @console.print(kind_of(Object(empty_map().with(key: "desk", value: Number(1)))))
```

`una run .` prints:

```text
nothing
yes or no: true
a whole number: 12
a decimal: 9.95
a text: lamp
a list of 2
an object of 1
```

## Specification

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

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).
