Keep your JSON valid
11th October 2006
I’m a big fan of JSON, and it’s great to see it turning up as an output option for so many Web APIs. Unfortunately, many of these APIs are getting the details slightly wrong and in doing so are producing invalid JSON.
JSON isn’t just the object-literal syntax of JavaScript; it’s a very tightly defined subset of that syntax. The site has a spec (illustrated with pretty state machine diagrams) and there’s an RFC as well.
By far the most common error I’ve encountered relates to object keys. In JSON (unlike in JavaScript) these MUST be double-quoted strings. In fact, ALL strings in JSON must be enclosed in double quotes (JavaScript also allows single quotes; JSON does not).
Valid:
{ "name": "Simon" }
Invalid:
{ name: "Simon" }
{ 'name': "Simon" }
{ "name": 'Simon' }
It’s worth reviewing the other key differences between JSON and JavaScript. Remember, all valid JSON is valid JavaScript but the opposite is not true; JSON is a subset.
This stuff matters. Python’s excellent simplejson module is a strict parser; it refuses to consume invalid JSON. If you’re building a JSON API it’s worth taking the time to ensure you are valid—I suggest installing Python and simplejson and trying the following:
$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
>>> import urllib, simplejson
>>> url = "http://your-api.org/your-api/json"
>>> simplejson.load(urllib.urlopen(url))
Try it against a few JSON supporting sites. You might be surprised at the amount of invalid JSON out there.
As with outputting XML, the best way to avoid these problems is to use a pre-existing JSON generation library rather than rolling your own. I’ve had good experiences with simplejson for Python and php-json for PHP.
More recent articles
- Weeknotes: the aftermath of NICAR - 16th March 2024
- The GPT-4 barrier has finally been broken - 8th March 2024
- Prompt injection and jailbreaking are not the same thing - 5th March 2024
- Interesting ideas in Observable Framework - 3rd March 2024
- Weeknotes: Getting ready for NICAR - 27th February 2024
- The killer app of Gemini Pro 1.5 is video - 21st February 2024
- Weeknotes: a Datasette release, an LLM release and a bunch of new plugins - 9th February 2024
- Datasette 1.0a8: JavaScript plugins, new plugin hooks and plugin configuration in datasette.yaml - 7th February 2024
- LLM 0.13: The annotated release notes - 26th January 2024
- Weeknotes: datasette-test, datasette-build, PSF board retreat - 21st January 2024