Basic JSON

JSON(5) data can be a few constants, number, string, array or objects. Commonly data is transferred as an object, a natural counterpart to an Idio hash table.

The JSON5 module is not loaded by default so you’ll need to import it.

Let’s suppose we have a nominal sliver of JSON in a file:

foo.json
{
  "key": "value",
  "array": [
    "of",
    4,
    true,
    "things"
  ]
}

Reading

The parse-file function is what we need, here:

import json5

ht := parse-file "foo.json"

hash-keys ht                 ; ("key" "array")

ht."key"                     ; "value"

ht."array"                   ; #[ "of" 4 #t "things" ]

You’ll notice from the "array" element that JSON’s true has become Idio’s #t.

If we already had a string, then we can call parse-string. Here, we’ll cheat by collecting the output from cat’ing the file:

import json5

str := (collect-output cat "foo.json")

ht := parse-string str

hash-keys ht                 ; ("key" "array")

If you look at str, you’ll see it has all of the original newlines embedded. It’s just a string, complicated by all the necessary escaping of double-quotes:

"{\n  \"key\": \"value\",\n  \"array\": [\n    \"of\",\n    4,\n    true,\n    \"things\"\n  ]\n}"

Generating

Generating JSON(5) is easy enough noting that the generate function (and generate-json function) create a string, you need to send it somewhere.

If we continue the previous example and change a couple of things:

ht."key" = "new value"
ht."array".1 = 3
ht."array".2 = #f

printf "%s\n" (generate ht)

should print out:

{
  "array": [
      "of",
      3,
      false,
      "things"
    ],
  "key": "new value"
}

Note the sequence, here. There is no implied ordering of members within an object.

Last built at 2026-01-04T22:40:02Z+0000 from da47fd3 (dev)