I'm writing some acceptance tests for a java webapp that returns JSON objects. I would like to verify that the JSON returned validates against a schema. Can anyone suggest any tools for this?
-
1A schema for JSON? Does such a thing exist? – skaffman Mar 23 '10 at 10:39
-
8[Yes](http://json-schema.org/) – quikchange May 23 '11 at 20:52
-
1There is a draft at IETF: http://tools.ietf.org/html/draft-zyp-json-schema-04 and implementations, for Java see https://github.com/fge/json-schema-validator – eskatos Oct 27 '13 at 14:48
3 Answers
The JSON Tools project (Programmer's Guide) includes a tool to validate the contents of a JSON file using a JSON schema.
An alternative could be to validate running the (JavaScript) JSON Schema Validator using Rhino.

- 83
- 1
- 4

- 13,058
- 6
- 46
- 60
-
3I'm betting a lot has changed on this subject since March 2010. What are your thoughts now? Are you using anything for JSON validation? Also, @Fredrik , how about yourself? – blong Nov 11 '11 at 20:21
The json-schema-validator (currently in version 0.0.1, so it's in a pre-alpha state) worked pretty well for me. Be aware that it is not 100% feature complete but it could still correctly identify a lot of errors in my json content.

- 416
- 3
- 6
-
The project behind is hosted on http://gitorious.org/json-schema-validation-in-java and still quite active – ngeek Jun 18 '12 at 07:53
-
Now it's version 2.2.6, but the author no longer maintains it and looks for some new guy to do that. However, it is probably the only option in Java at the moment. – Vlasec Apr 08 '15 at 08:30
-
2Both of the above links are old. I found it here: [https://github.com/daveclayton/json-schema-validator](https://github.com/daveclayton/json-schema-validator) – mojoken Dec 08 '16 at 21:42
@b.long I came across this post looking for a very particular solution to easily verify if a String's content has a JSON (object/array). I couldn't find any library that would suit my needs.
JSON Tools project or json-schema-validator though offer a lot are too big for my needs. Thus I did implement my own solution that is build on top of JSON.org reference implementation of JSON in Java. I have already been using the JSON.org's code and technically all this functionality was already there, thus I added this very simple functionality reusing its code.
I use it to easily test if the String returned by methods querying my database are formatted in proper JSON which otherwise if badly formatted and posted to a client might cause it to stop working.
Hope this is of use to you as it is to me.

- 7,913
- 4
- 43
- 85