JSON in Python and Common Lisp

Both, Python and Common Lisp have libraries for writing and reading JSON format. JSON can be used as an alternative to XML, when not only machines will read the data. XML can be hard to read when you have more tags than data. JSON does not have the overhead of start and end tags.

In Python, you can import module json from the Python standard library.

>>> import json
>>> a = [1, 2, 3]
>>> json.dumps(a)
‘[1, 2, 3]’
>>> json.loads(‘[1, 2, 3]’)
[1, 2, 3]

In Common Lisp, you can use the CL-JSON library.

CL-USER> (ql:quickload “CL-JSON”)
CL-USER> (cl-json:encode-json ‘(1 2 3))
CL-USER> (with-input-from-string (s “[1, 2, 3]”) (cl-json:decode-json s))
(1 2 3)

These are just very simple examples, but you see that usage is straightforward. If you don’t like the syntax of JSON, you might want to have a look at YAML. JSON is a subset of YAML version 1.2.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s