jsown
A fast JSON parser and generator.

Introduction

jsown is a high-performance Common Lisp JSON parser. Its aim is to allow for the fast parsing of JSON objects into Lisp s-expressions, which can then be edited or accessed like any other Lisp code, and re-encoded into JSON.

Installation

jsown is available via Quicklisp. To install, evaluate:

CL-USER> (ql:quickload :jsown)

Basic Usage

Assume we have a dynamic variable *json-string* which, as the name helpfully suggests, contains an encoded JSON string:

CL-USER> (defvar *json-string* "{\"foo\":\"bar\",\"baz\":[1,2,\"blub\"]}") => *JSON-STRING*

To convert this JSON string into jsown's internal representation, use the PARSE function:

CL-USER> (jsown:parse *json-string*) => (:OBJ ("foo" . "bar") ("baz" 1 2 "blub"))

jsown decodes JSON Objects into lists whose first member is the :OBJ keyword symbol. Each subsequent sub-list represents a key-value association as a CONS-ed pair; thus, for each sub-list, the members of the JSON object can be accessed with CAR and CDR to retrieve the key and value, respectively.

JSON Arrays are also decoded into lists, though without the leading :OBJ symbol. Rather than CONS-ed pairs, decoded arrays are proper lists (lists whose last element is itself a list). This can result in seemingly ambiguous Lisp representations of data; for example, in the above code, the key-value pair "baz": [1, 2, "blub"] is parsed as ("baz" 1 2 "blub"). This may appear like a decoded array at first, but as this is a sub-list of an object, it can be separated into key and value by the usual combination of CAR and CDR.

From here, we can convert this internal representation back into valid JSON using the TO-JSON method:

CL-USER> (defvar *json-object* (jsown:parse *json-string*)) => *JSON-OBJECT* CL-USER> (jsown:to-json *json-object*) => "{\"foo\":\"bar\",\"baz\":[1,2,\"blub\"]}"

We can also use the VAL function to retrieve values from jsown objects:

CL-USER> (jsown:val *json-object* "foo") => "bar"

External Links

The project's repository on GitHub

Reference Manual

License

MIT