BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JSON Validation Roundup

JSON Validation Roundup

This item in japanese

Lire ce contenu en français

Bookmarks

Create any “flexible” or “extensible” file format and sooner or later a group of developers will start complaining about validation. There are many this can be handles, but in general they can be categorized as:

  • Assume the data is correctly formatted.
  • Manually check for mal-formed data and attempt to correct.
  • Manually check for mal-formed data and reject where appropriate.
  • Automatically check for mal-formed data.

The purpose of this article isn’t to debate these options, but rather to summarize the toolkits available for automatic validation.

JSON Schema

JSON Schema is a proposed standard supported by 17 different projects. It is available for JavaScript, Java, Python, Ruby, Perl, PHP, .NET, ActionScript, C, Haskell, and Erlang. The format is more complicated than VeriJSON with the schema bearing little resemblance to the actual data. It is also more comprehensive, with the ability to range-check numbers, limit lists, and prevent duplicate entries in lists. It also supports references to other schemas, allowing you to break down large schemas into smaller components.

Atdgen

Atdgen offers JSON serialization and deserialization support to OCaml. JSON schemas are created using what are known as atd files. These files are run through a code generator to create OCaml class files and matching object/JSON converters. In Atdgen, validation occurs during the deserialization process.

Validation rules in atd files are open-ended, developers can inject whatever OCaml code they want directly into the atd file. References to secondary schema files are supported.

Atdgen is available under an open source license.

DataContractJsonSerializer

Based on WCF’s DataContractSerializer, the DataContractJsonSerializer uses class definitions and data contract attributes to determine how a JSON file is validated and ultimately deserialized. This is suitable for basic tasks, but tends to exhibit performance problems and has difficulty with dictionaries. Validation is pretty much limited to basic structure.

Json.NET

Json.NET by James Newton-King is by far the most comprehensive JSON serializer/deserializer available today. Like the DataContractJsonSerializer, Json.NET can use classes to define its schema. You can either use the standard WCF data contract attributes or JSON.NET’s serialization attributes and supports a wide variety of types including DataTable and nHibernate entities. And as of this month, JSON.NET 5.0 supports unlimited size integers (via BigInteger) and read-only collections that expose an IEnumerable constructor.

JSON.NET also supports validation via JSON Schema.

VeriJSON

VeriJSON is an Objective-C library that relies on pattern-based matching. The patterns themselves are written in JSON. Supported types are “number”, “string”, “bool”, and “url”. Strings can be restricted using a regular expression and urls by schema (e.g. http, ftp). Arrays and objects are represented structurally and optional properties are supported.

VeriJSON is available via an open source license.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • CAMV with JSON validation engine

    by David Webber,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    We just released the open source CAM editor v3.0 with new support for JSON validation. The CAMV rules validation engine is combined with StAXON to provide XPath based validation of JSON content. You can see this working in this 9 minute quick introductory video:
    www.youtube.com/watch?v=K7-JlYGsjEc

    And then the main project page is here: www.cameditor.org

    We have more examples on our VerifyXML.org site and the associated GitHub repository.

    Enjoy!

  • To validate json online

    by James Malvi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Nice tool to validate your json data codebeautify.org/jsonviewer

  • Re: To validate json online

    by Khaven Parker,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    There are lots of validators out there. I use beautifytools.com/json-validator.php

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT