Skip to the content.

json

import "json" as json

A JSON encoder and decoder written in Argon.

json.stringify(value, pretty=false, indent=" ")

Converts a value to a JSON string. Supported values are null, booleans, numbers, strings, arrays, tuples (written as JSON arrays) and dictionaries:

import "json" as json

term.log(json.stringify(null))                    # null
term.log(json.stringify([1, 2, 3]))               # [1,2,3]
term.log(json.stringify({a: 1, b: [true, "x"]}))  # {"a":1,"b":[true,"x"]}

With pretty=true, the output is spread over multiple lines and indented with indent:

import "json" as json

term.log(json.stringify({name: "argon", tags: ["fast"]}, pretty = true, indent = "  "))
{
  "name": "argon",
  "tags": [
    "fast"
  ]
}

Details:

json.parse(text)

Parses a JSON string into Argon values. Objects become dictionaries, so their members can be read with .:

import "json" as json

let data = json.parse("{\"name\": \"argon\", \"versions\": [3, 4], \"stable\": null}")
term.log(data.name, data.versions[1], data.stable)   # argon 4 null

All standard JSON escapes are supported, including \uXXXX and surrogate pairs.

Malformed JSON throws a ValueError describing the problem:

import "json" as json

try json.parse("[1, 2,]")
catch (ValueError as e) term.log(e.message)   # Unexpected character ']' at position 6

Next