std.json
std.json lets your program speak JSON — the format nearly every web API uses to exchange data. parse turns JSON text into a map; stringify goes the other way.
std.json lets your program speak JSON — the format nearly every web API uses to exchange data. parse turns a JSON text into a map you can fetch values from, and stringify goes the other way, from map to text you can send on.
Instead of picking out fields by hand, the module gives you safe hent_* functions that fetch a value by key and return a sensible default if the key is missing. That means an unexpected response does not topple the program — you get an empty text or a zero instead of a crash.
When to use it
Use std.json when you talk to web APIs or store structured data as text. parse takes a response and gives you a map; the hent_* functions pick out the values safely, with sensible defaults if a key is missing.
Getting started
bruk std.json som json
// kropp could be, for example, {"navn": "Ada", "alder": 36}
la data = json.parse(kropp)
la navn = json.hent_tekst(data, "navn") // "Ada"
la alder = json.hent_tall(data, "alder") // 36
// build JSON back again
la svar = json.stringify(data)parse gives you a map, and hent_* picks out the values safely. If a key is missing, you get an empty text or zero instead of an error.
The functions
The module has two directions and a group of helpers. parse and stringify convert between text and map. hent_tekst, hent_tall and hent_bool pick out single values safely, and hent_array_ and array_til_ handle lists.
parse(innhold: tekst) -> ordbok_tekst— Interprets a JSON text and returns a map you can fetch values from.stringify(data: ordbok_tekst) -> tekst— Turns a map into a JSON text with correct escaping.hent_tekst(data: ordbok_tekst, nøkkel: tekst) -> tekst— Fetches a text value atnøkkel, or empty text if it is missing.hent_tall(data: ordbok_tekst, nøkkel: tekst) -> heltall— Fetches a numeric value atnøkkel, or 0 if it is missing.hent_bool(data: ordbok_tekst, nøkkel: tekst) -> bool— Fetches a boolean value atnøkkel.array_til_tekst_liste(data: ordbok_tekst) -> liste_tekst— Turns a JSON array into aliste_tekst.array_til_tall_liste(data: ordbok_tekst) -> liste_heltall— Turns a JSON array into aliste_heltall.hent_array_tekst(data: ordbok_tekst, nøkkel: tekst) -> liste_tekst— Fetches an array of text directly at a key.hent_array_tall(data: ordbok_tekst, nøkkel: tekst) -> liste_heltall— Fetches an array of numbers directly at a key.
Good to know
- The
hent_*functions return a default on a missing key, so an unexpected response does not topple the program. - Nested structures are fetched step by step;
hent_array_tekstandhent_array_tallgive you lists directly. - If you build JSON to send out, let
stringifyhandle correct escaping instead of assembling the text yourself.
std.json is one of the modules in the standard library. Everything ships with the runtime — no installation, no external dependencies. See also Documentation for the language and the runtime.
Related
- std.tekststd.tekst is the toolbox for everything to do with text — searching and checking, splitting and joining, trimming whitespace, changing case, and converting to and from numbers.
- std.httpstd.http is a client for talking to other services over HTTP and HTTPS. You can fetch data, send data, and interpret the response — including JSON — without building the request by hand.
- std.dbstd.db gives you access to databases with a small, familiar set of functions: open a connection, run SQL, fetch values and rows, and control transactions.