You can validate JSON in tons of different ways. And it’s become more and more popular to create your own JSON validator implementation. But I’m here to tell you that you can spend your time in much better ways!
That’s because JSON validation has a standard simply called JSON schema. Granted, the standard is only in draft 4 and has actually expired, but it continues to receive a lot of support.
JSON schema has implementations in many programming languages. In Node.js in particular, there is a whole range of implementations, which makes it hard to select the best one.
Which implementation is the fastest? Which implementation has the fewest errors when run against the official JSON schema test suite?
Comparing implementations against each other becomes easy when they all implement the same standard. They can compete on equal footing. But it still requires a third party to do the comparison.
Choose the fastest implementation
To make it easier to select an implementation in Node.js, I have built an Open Source benchmark.
Here are the performance results:
The chart clearly shows that is-my-json-valid
is by far the most performant implementation. It’s 5x faster than No. 2 and 1000x faster than the slowest implementation!
Choose the implementation that is most standards-compliant
If we then run the implementations against the official JSON schema test suite, we also see differences in how well the implementations do.
Below is a chart detailing the number of official tests that fail for each implementation:
Note that for some of the implementations, a conscious choice has been made not to implement parts of the standard. You can find more details on this on the various implementation websites:
is-my-json-valid, themis, z-schema, jjv, skeemas, jayschema, jsck, jassi, JSV, request-validator, json-gate, json-model, tv4, jsonschema, revalidator.
Open Source and standards
The standards allow us to compare the implementations using a benchmark. During the benchmarking I came across errors in several of the implementations. I’ve reported these errors as issues at the github repositories of the respective implementation, and a lot of the errors have already been fixed.
Also, I’ve made the creators of the validators aware of each others’ implementations, which has led to more collaboration and discussions.
You can always find the latest results and more details in the benchmarking.
So, let me know: How do you validate your JSON?