(rontolisp) docs
← rontolisp Package Functions

rontolisp:json-parse

(rontolisp:json-parse string)

Parses a JSON document string into Lisp values, following the defaults of the com.inuoe.jzon library: a JSON object becomes a hash table with string keys, an array a vector, and true/false/null become t, nil and the symbol null. rontolisp:json-parse is a lightweight subset of jzon, so a program can start here and later switch to jzon without changing shape — with a single deliberate exception, the wide-integer rule noted below.

Switch to com.inuoe.jzon when you outgrow the subset: for its richer features (pretty-printing, a streaming writer, a :replacer, custom serialization), or to make the JSON code portable to other Common Lisp implementations — com.inuoe.jzon is a standard library, while rontolisp:json-* runs only on rontolisp.

Value mapping

JSONLisp
objecthash table with string keys (equal test)
arrayvector
stringstring (\uXXXX escapes and surrogate pairs are decoded)
numberinteger, or float when it has a fraction, an exponent or more than 18 digits
truet
falsenil
nullthe symbol null

The one incompatibility with jzon

Integers wider than 18 digits become floats on every backend -- a shared library rule that keeps the parse identical across all backends. jzon instead keeps them as exact integers of any width, so this is the single point where rontolisp:json-parse and jzon:parse disagree — a 13-digit millisecond timestamp parses exactly on both, but a 19-digit integer parses as a float here and as an exact integer under jzon. Everything else round-trips identically.

Errors

Invalid JSON and trailing characters after the value signal an error when json-parse is called:

CL-USER> (rontolisp:json-parse "{\"a\": ")
Error: json-parse: unexpected end of input
CL-USER> (rontolisp:json-parse "1 2")
Error: json-parse: unexpected trailing characters

Limitations

  • A JSON object always parses to a hash table, so {} (an empty hash table) is distinct from false/nil, from an empty array #(), and from the null symbol — unlike JavaScript, the four are never conflated.
  • On the WASM backends a float with magnitude 2³¹ or larger parses correctly but cannot be printed (print/princ-to-string trap); see the WASM guide.
  • On the wasm-GC backends the strings it answers are immutable, as a string literal is: (setf (char s 0) #\x) on one makes a new string and rebinds s, so the table holding the old one does not see the change. The interpreter and the JVM answer mutable strings.

Backend support

Works on every backend and in every WASM mode (Preview 1 included). The interpreter and the JVM run a parser written in rontolisp itself; the wasm-GC backends compile a native one into the program when it is used. The typical use is parsing a rontolisp:fetch response body:

(print (gethash "url"
                (rontolisp:json-parse
                 (getf (rontolisp:await (rontolisp:fetch "https://httpbin.ik.am/get")) :body))))   ; "https://httpbin.ik.am/get"

The inverse operation is rontolisp:json-stringify. To read a few values of a large document, or to edit it, without making Lisp values of all of it, use rontolisp:json-value and rontolisp:json-span.