json Library Reference

The json module

Constants

$null Constant

Is what “null” parses to.

Conditions

<json-error> Open Instantiable Class

All JSON errors are subclasses of this class.

Superclasses:

<format-string-condition> <error>

<json-parse-error> Instantiable Class

Any error signalled during parsing (except for file system errors) will be an instance of this.

Superclasses:

<json-error>

parse-json

parse-json Open Generic function

Parse json formatted text from the given source. This is the main user-visible entry point for parsing. table-class, if provided, should be a subclass of <table> to use when creating a json “object”.

Signature:

parse-json (source, #key strict?, table-class) => (json)

Parameters:
  • source – An <object>.

  • strict? (#key) – An instance of <boolean>.

  • table-class (#key) – Default to <string-table>.

Values:
Discussion:

The parse is strict by default. If strict?: #f is used then:

  • # is allowed as a comment character

  • ”<c>” is equivalent to <c>, where <c> is not a defined escape character.

  • trailing commas are allowed in arrays and objects

parse-json(<string>) Method

Parse a JSON object from a <string>.

Signature:

parse-json (source, #key strict?, table-class) => (json)

Parameters:
  • source – An instance of <string>

  • strict? (#key) – An instance of <boolean>. Default to #t.

  • table-class (#key) – A subclass of <table>.

Values:
Example:
let data = """{"a": 1, "b": 2,}""";
let parsed = parse-json(data, strict?: #f);
let a = parsed["a"];

Run this example in https://play.opendylan.org

Note the use of strict?: #f is needed since data has a trailing comma after the number 2.

parse-json(<stream>) Method

Parse a JSON object from a <stream>.

Signature:

parse-json (source, #key strict?, table-class) => (json)

Parameters:
  • source – An instance of <stream>.

  • strict? (#key) – An instance of <boolean>. Default to #f.

  • table-class (#key) – A subclass of <table>.

Values:
Example:
with-open-file (fs = "data.json")
  let data = parse-json(fs, strict?: #f);
  ...
end;

Run an example with a string stream in https://play.opendylan.org