Justify is a JSON validator based on the JSON Schema specification and Java API for JSON Processing (JSR 374).
Clone or download

README.md

Justify

Apache 2.0 License Maven Central Javadocs Build Status

Justify is a JSON validator based on JSON Schema Specification and Java API for JSON Processing (JSR 374).

Main Features

  • Compliant with JSON Schema Specification Draft-07.
  • Reinforces Java API for JSON Processing (JSR 374) transparently with the validation functionality.
  • Can be used with Java API for JSON Binding (JSR 367) via a custom JsonProvider.
  • Reports problems with the source locations including line and column numbers.
  • Passes 1,000+ test cases including official ones provided by JSON Schema Test Suite.
  • Accepts custom formats for string and other instance types.
  • Supports Java 8, 9, 10 and 11, can be used as a modular jar in Java 9 and higher.
  • Internationalized problem messages, including Japanese language support.

Getting Started

Minimum Setup

This software is available in the Maven Central Repository. In runtime the library requires one of Java API for JSON Processing (JSR 374) implementations. If your choice is Reference Implementation of the API, the following two dependencies are all you need to add to your pom.xml.

<dependency>
  <groupId>org.leadpony.justify</groupId>
  <artifactId>justify</artifactId>
  <version>0.10.0</version>
</dependency>

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.json</artifactId>
  <version>1.1.3</version>
</dependency>

Alternatively, the latter dependency can be replaced with other implementation such as Apache Johnzon as below.

<dependency>
  <groupId>org.apache.johnzon</groupId>
  <artifactId>johnzon-core</artifactId>
  <version>1.1.10</version>
</dependency>

Using with the Streaming API of JSON Processing

JsonValidationService service = JsonValidationService.newInstance();

// Reads the JSON schema
JsonSchema schema = service.readSchema(Paths.get("news.schema.json"));

// Problem handler which will print problems found.
ProblemHandler handler = service.createProblemPrinter(System.out::println);

Path path = Paths.get("fake-news.json");
// Parses the JSON instance by javax.json.stream.JsonParser
try (JsonParser parser = service.createParser(path, schema, handler)) {
    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        // Do something useful here
    }
}

Using with the Object Model API of JSON Processing

JsonValidationService service = JsonValidationService.newInstance();

// Reads the JSON schema
JsonSchema schema = service.readSchema(Paths.get("news.schema.json"));

// Problem handler which will print problems found.
ProblemHandler handler = service.createProblemPrinter(System.out::println);

Path path = Paths.get("fake-news.json");
// Reads the JSON instance by javax.json.JsonReader
try (JsonReader reader = service.createReader(path, schema, handler)) {
    JsonValue value = reader.readValue();
    // Do something useful here
}

Command-Line Interface

Justify CLI, a command-line utility for validating JSON documents without any coding is also available. The latest stable distribution can be downloaded from Releases in tar.gz or zip format, whichever you prefer.

After unpacking the downloaded file, just typing the following command validates a JSON instance against a JSON schema.

$ ./justify.sh <path/to/JSON/schema> <path/to/JSON/instance>

Or for Windows:

> justify.bat <path/to/JSON/schema> <path/to/JSON/instance>

The utility is tested under Java 8 and higher.

Additional Resources

Current Development Status

Schema keywords implemented

  • type
  • enum
  • const
  • multipleOf
  • maximum/exclusiveMaximum
  • minimum/exclusiveMinimum
  • maxLength
  • minLength
  • pattern
  • items
  • additionalItems
  • maxItems
  • minItems
  • uniqueItems
  • contains
  • maxProperties
  • minProperties
  • required
  • properties
  • patternProperties
  • additionalProperties
  • dependencies
  • propertyNames
  • if/then/else
  • allOf
  • anyOf
  • oneOf
  • not
  • definitions
  • title
  • description
  • format
    • date-time/date/time
    • email (compliant with RFC 5322)
    • idn-email (compliant with RFC 6531)
    • hostname (compliant with RFC 1034)
    • idn-hostname (compliant with RFC 5890)
    • ipv4 (compliant with RFC 2673)
    • ipv6 (compliant with RFC 4291)
    • json-pointer (compliant with RFC 6901)
    • relative-json-pointer
    • uri/uri-reference (compliant with RFC 3986)
    • iri/iri-reference (compliant with RFC 3987)
    • uri-template (compliant with RFC 6570)
    • regex (compliant with ECMA 262)
  • contentEncoding, with built-in "base64" support.
  • contentMediaType, with built-in "application/json" support.

Schema keywords not implemented yet

  • default

Similar Solutions

There exist several JSON validator implementations conformant to the JSON Schema Specification, including those for other programming languages. The list of implementations is available on the JSON Schema web site.

Copyright Notice

Copyright © 2018-2019 the Justify authors. This software is licensed under Apache License, Versions 2.0.