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
| JSON | Keel |
|---|---|
| object | Record |
| array | List |
| string | String |
| number (int) | Int |
| number (float) | Float |
| boolean | Bool |
| null | Unit |
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.
import Json
let result: Result { name: String } String = Json.parse "{\"name\": \"Bob\"}"Try itNotes: 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.
import Json
Json.parseString "\"hello\""
-- Ok "hello"Try itNotes: 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.
import Json
Json.parseInt "42"
-- Ok 42Try itNotes: 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.
import Json
Json.parseFloat "3.14"
-- Ok 3.14Try itNotes: 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.
import Json
Json.parseBool "true"
-- Ok TrueTry itNotes: 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.
import Json
Json.encode { name = "Alice" }
-- "{\"name\":\"Alice\"}"Try itNotes: 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.
import Json
Json.encodePretty 2
{ x = 1 }Try itNotes: 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.
import Json
Json.get "name" "{\"name\":\"Bob\"}" -- Ok "\"Bob\""Try itNotes: Returns the field value as a JSON-encoded string.
See also: Json.parse