Esc
Start typing to search...

Json Module

JSON parsing and encoding.

The Json module provides functions for converting between JSON strings and Keel values. JSON objects map to records, arrays to lists, strings/numbers/booleans to their Keel equivalents, and null to Unit.

Common patterns

import Json

-- Parse JSON (type annotation required for generic parse)
let data: Result { name: String, age: Int } String = Json.parse "{\"name\": \"Bob\", \"age\": 30}"

-- Type-specific parsers don't need annotations
let n = Json.parseInt "42"

-- Encode Keel values to JSON
let jsonStr = Json.encode { x = 1, y = 2 }

-- Extract a field from JSON
let name = Json.get "name" "{\"name\": \"Bob\"}"

Type mapping

JSONKeel
objectRecord
arrayList
stringString
number (int)Int
number (float)Float
booleanBool
nullUnit

Functions

Parsing

Json.parse

String -> Result a String

Parse a JSON string into a Keel value. Objects become records, arrays become lists. Requires a type annotation on the result because the return type is generic.

Example:
import Json
let result: Result { name: String } String = Json.parse "{\"name\": \"Bob\"}"
Try it

Notes: A type annotation is required (e.g., let x: Result MyType String = Json.parse ...). JSON null maps to Unit, numbers to Int or Float. Use parseInt, parseString, etc. when you know the expected type.

See also: Json.encode, Json.parseInt, Json.parseString

Json.parseString

String -> Result String String

Parse a JSON string and return only the string value.

Example:
import Json

Json.parseString "\"hello\""

-- Ok "hello"
Try it

Notes: Returns Err if the JSON value is not a string.

See also: Json.parse, Json.parseInt

Json.parseInt

String -> Result Int String

Parse a JSON string and return only the integer value.

Example:
import Json

Json.parseInt "42"

-- Ok 42
Try it

Notes: Returns Err if the JSON value is not an integer.

See also: Json.parse, Json.parseFloat

Json.parseFloat

String -> Result Float String

Parse a JSON string and return only the float value.

Example:
import Json

Json.parseFloat "3.14"

-- Ok 3.14
Try it

Notes: Returns Err if the JSON value is not a number.

See also: Json.parse, Json.parseInt

Json.parseBool

String -> Result Bool String

Parse a JSON string and return only the boolean value.

Example:
import Json

Json.parseBool "true"

-- Ok True
Try it

Notes: Returns Err if the JSON value is not a boolean.

See also: Json.parse

Encoding

Json.encode

a -> String

Encode a Keel value as a compact JSON string.

Example:
import Json

Json.encode { name = "Alice" }

-- "{\"name\":\"Alice\"}"
Try it

Notes: Records become objects, lists become arrays.

See also: Json.encodePretty, Json.parse

Json.encodePretty

Int -> a -> String

Encode a Keel value as pretty-printed JSON with the given indentation.

Example:
import Json

Json.encodePretty 2
{ x = 1 }
Try it

Notes: The Int argument specifies the number of spaces per indent level.

See also: Json.encode

Access

Json.get

String -> String -> Result String String

Get a field value from a JSON string as a JSON string.

Example:
import Json
Json.get "name" "{\"name\":\"Bob\"}"  -- Ok "\"Bob\""
Try it

Notes: Returns the field value as a JSON-encoded string.

See also: Json.parse