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
- AI for Data Journalism: demonstrating what we can do with this stuff right now - 17th April 2024
- Three major LLM releases in 24 hours (plus weeknotes) - 10th April 2024
- Building files-to-prompt entirely using Claude 3 Opus - 8th April 2024
- Running OCR against PDFs and images directly in your browser - 30th March 2024
- llm cmd undo last git commit - a new plugin for LLM - 26th March 2024
- Building and testing C extensions for SQLite with ChatGPT Code Interpreter - 23rd March 2024
- Claude and ChatGPT for ad-hoc sidequests - 22nd March 2024
- 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