tl;dr: Use json.parser.online.fr to check your hand-rolled JSON, and remember that JSON embedded in .js code lets you get away with not quoting your keys.
I'm making a new toy web-based database. I regularly use this ancient Perl CGI db I constructed a decade ago to hold information online; before I started this blog it held my devnotes, it holds my list of media watched, it used to hold my list of possible future blog entries, etc. (Now I use the awesome app "simplenote" for that, since it syncs to my devices and I can get to and update the information even when I don't have net access, like on the subway.) Overall the simple HTML form based, CGI/flatfile database has held up pretty well, but for "self education" as well as this porches project I'm on, I'd like to make something similar to it, but using json, and more javascript.
Eventually I will have a trivial CRUD protocol, and can swap out the front end (would be a good angular project) and the backend (would be a good node.js project). For now it's a crude jQuery-based UI and an even cruder hand-rolled JSON-in-Perl (couldn't get the JSON module working, sadly) on the backend.
ANYWAY, building up the toy, I started with a "mockDB" that implemented the middleware interface by just keeping information in the browser. When I went to construct the Perl backend, I grabbed the JSON I was using in the mock and put it into individual .json files. Luckily, The Ghost of Annoying Bugs Past tapped me on the shoulder, and before I used the resulting CGI output I ran it through json.parser.online.fr and it came up all red. Quickly I sussed the issue, having seen it before: when you build some static json in a .js file, you don't have to quote your keys, i.e. you can do something like:
var map = { key1: "foo", key2: "bar" };
That works but is not proper JSON, which should be
var map = { "key1": "foo", "key2": "bar" };
(The former only ever works because Javascript has this weird-ish concept of labels, which can act like strings as well.)
To quote F.P. Jones:
Experience is that marvelous thing that enables you recognize a mistake when you make it again.
No comments:
Post a Comment