Skip to content
NorscodeNorscode

std.json

ReferenceBy the Norscode project

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 at nøkkel, or empty text if it is missing.
  • hent_tall(data: ordbok_tekst, nøkkel: tekst) -> heltall — Fetches a numeric value at nøkkel, or 0 if it is missing.
  • hent_bool(data: ordbok_tekst, nøkkel: tekst) -> bool — Fetches a boolean value at nøkkel.
  • array_til_tekst_liste(data: ordbok_tekst) -> liste_tekst — Turns a JSON array into a liste_tekst.
  • array_til_tall_liste(data: ordbok_tekst) -> liste_heltall — Turns a JSON array into a liste_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_tekst and hent_array_tall give you lists directly.
  • If you build JSON to send out, let stringify handle 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

Back to the overview