NSJSONSerialization delights in an opaque “lolnope character 12341234” error message that provides zero context. It doesn’t even bother to take advantage of line numbers to help you out, even if the JSON data has linebreaks. This is a royal pain, especially if you’re working with hand-written stub data for a web service.
Install it via
npm install jsonlint -g, and you’ll be able to find and fix
syntax errors in JSON far faster than you would puzzling over
NSJSONSerialization’s error message.
Comparing Parser Error Messages
1 2 3 4 5 6 7 8 9
The wonderfully useless NSJSONSerialization error:
1 2 3 4 5 6 7 8 9 10 11 12 13
The equally useless Python json error:
1 2 3 4 5 6 7 8 9 10
Hey, at least Python takes advantage of the fact the file has line numbers.
The jsonlint output:
1 2 3 4 5
Now, isn’t that nice? No need to cross-reference to the file, since there’s context right there, and you know exactly what it’s expecting, rather than just some opaque “delimiter”.
(The Cocoa error in this example isn’t entirely useless, since it does call out that a key is missing its value. I’ve seen more useless ones where it faults the very end of the file for something that went wrong way earlier.)
I’ve installed it locally using
npm install jsonlint -g.
Apparently, you could also use http://jsonlint.com/, but:
- I’m wary of posting potentially sensitive JSON data to a public web service
- I like being able to work even without network access
- command-line tools compose easily into pipe-lines far better than most websites
So, I naturally recommend you just install it locally.