JSON schema validation using java [closed]

2019-01-13 18:28发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

@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.