34

Is there a stable library that can validate JSON against a schema?

json-schema.org provides a list of implementations. Notably C and C++ are missing.

Is there a reason I can't easily find a C++ JSON schema validator?
Doesn't anyone else want a quick way to validate incoming JSON files?

Community
  • 1
  • 1
deft_code
  • 57,255
  • 29
  • 141
  • 224
  • 4
    Most likely because C and C++ are rarely used for webapps, which is the predominant application of JSON. If there aren't any, you could take another one and port it. – Kaleb Brasee Jan 13 '11 at 02:51
  • 6
    JSON can (is?) used for much more than talking between browsers and webservers. You might want to use it storing preferences, RPC between systems, etc. Basically, anywhere xml is (mis)used today. – Mark Renouf Jan 23 '11 at 14:51
  • 3
    @Mark: indeed this is our exact use case. We need a way to store complex configuration (too complex for ini). We chose JSON as over xml. – deft_code Jan 24 '11 at 15:37
  • 1
    The list of implementations now include C and C++: http://json-schema.org/implementations.html#validator-cpp – Ida Oct 10 '17 at 08:23

7 Answers7

18

Is there a stable library that can validate JSON against a schema?

I found a couple hits on google:

You could also plug a Python or Javascript interpreter into your app, and simply run the native version of those validator implementations that you've already found.

Is there a reason I can't easily find a C++ JSON schema validator?

I believe JSON originated as a web technology, and C/C++ has fallen out of favor for web app implementation.

Eric
  • 5,137
  • 4
  • 34
  • 31
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 1
    The Chromium validator looks really good but I'd want to extract the schema from the chromium's base code before using it. So it's not a drop in solution but much better than writing one myself. – deft_code Jan 13 '11 at 15:29
  • The [JSON Schema site](http://json-schema.org/implementations.html) lists a C library (WJElement), but I don't have any experience with it, and I'm pretty sure it doesn't support the latest version of the standard. – cloudfeet Apr 18 '13 at 10:05
8

Valijson is a very good library which depends only on Boost (And I'm actually hoping to change that). It doesn't even depend on any particular JSON parser, providing adapters for most commonly-used libraries like JsonCpp, rapidjson and json11.

The code may seem verbose, but you can always write a helper (example for JsonCpp):

#include <json-cpp/json.h>
#include <sstream>
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validation_results.hpp>
#include <valijson/validator.hpp>

void validate_json(Json::Value const& root, std::string const& schema_str)
{
  using valijson::Schema;
  using valijson::SchemaParser;
  using valijson::Validator;
  using valijson::ValidationResults;
  using valijson::adapters::JsonCppAdapter;

  Json::Value schema_js;
  {
    Json::Reader reader;
    std::stringstream schema_stream(schema_str);
    if (!reader.parse(schema_stream, schema_js, false))
      throw std::runtime_error("Unable to parse the embedded schema: "
                               + reader.getFormatedErrorMessages());
  }

  JsonCppAdapter doc(root);
  JsonCppAdapter schema_doc(schema_js);

  SchemaParser parser(SchemaParser::kDraft4);
  Schema schema;
  parser.populateSchema(schema_doc, schema);
  Validator validator(schema);
  validator.setStrict(false);
  ValidationResults results;
  if (!validator.validate(doc, &results))
  {
    std::stringstream err_oss;
    err_oss << "Validation failed." << std::endl;
    ValidationResults::Error error;
    int error_num = 1;
    while (results.popError(error))
    {
      std::string context;
      std::vector<std::string>::iterator itr = error.context.begin();
      for (; itr != error.context.end(); itr++)
        context += *itr;

      err_oss << "Error #" << error_num << std::endl
              << "  context: " << context << std::endl
              << "  desc:    " << error.description << std::endl;
      ++error_num;
    }
    throw std::runtime_error(err_oss.str());
  }
}
ansgri
  • 2,126
  • 5
  • 25
  • 37
  • 1
    I agree that this seemed like a promising option, but it's not at all clear that this library is being supported anymore. – ddavella Oct 22 '18 at 18:32
  • 1
    Since you've posted this answer, it looks like Boost is now an optional dependency: https://github.com/tristanpenman/valijson#dependencies. It looks like that changed around August of 2019: https://github.com/tristanpenman/valijson/issues/63. @ansgri You were ahead of your time with your suggestion :) – GeminiDakota Aug 06 '20 at 14:41
2

You can try UniversalContainer (libuc). http://www.greatpanic.com/code.html. You're looking for the container contract/schema checking class in this library. The schema format is clunky, but should handle everything you care about and provide reasonable reporting on why a particular instance fails to meet the schema.

user693653
  • 19
  • 2
1

Is there a stable library that can validate JSON against a schema?

Rapidjson

I'm using it for JSON validation against a schema (for the most). It looks like to be tested and stable (v1.1.0 looks like to be latest release, according to the github repo).

bitfox
  • 2,281
  • 1
  • 18
  • 17
0

If you can accommodate a polyglot approach, Ajv appears to be a solid JavaScript implementation.

https://ajv.js.org/

Note: There is also an ajv-cli

https://github.com/jessedc/ajv-cli

0

On Windows platform you can use JSON Essentials for COM/ActiveX regardless of the language of your choice. If you want to use it from C++ using MSVC compiler you can have compiler generated wrappers using the #import pre-processor directive.

Its documentation contains a number of samples for the most common use cases in C#, VB and JavaScript. The language syntax is the only things that changes, it all can be implemented using the same library calls in pretty much any language that supports COM.

It's also worth to mention that it offers free licensing options.

Here is a sample of creating a schema, loading and validating a JSON document using the newly created schema for Windows Script Host (WSH) using JSON Essentials evaluation version:

// Create a new JsonEssentials class instance.
var jess = new ActiveXObject('JsonEssentials_0.JsonEssentials.1');
jess.unlock('jesscomeval');

// Create schema JSON document object.
var schemaDoc = jess.createDocument();

// Load JSON schema.
schemaDoc.load({
    'type': 'array',
    'items': {
        'type': 'integer'
    }
});

// Create schema collection object.
var schemas = jess.createSchemas();

// Add the test schema to the collection.
var schema = schemas.add(schemaDoc, 'uri:test');

// Create JSON document object.
var instanceDoc = jess.createDocument();

// Load JSON, an array of numeric values.
instanceDoc.load([ 0, 1, 2 ]);
 
// Validate the test JSON against the added schema.
var status = schema.validate(instanceDoc);

WScript.Echo(status.success ? 'validated' : 'validation failed');
Alexander
  • 471
  • 1
  • 4
  • 6
0

You can have a look to this approach https://epub.jku.at/obvulioa/content/pageview/6390647?query=berardinelli

The in-memory representation of your JSON documents (metaschema, schemas, or schema instances) is provided in EMF (Eclipse Modeling Framework). Once you have this in-memory representation (preserving the usual JSON textual notation, so compliant with any JSON tool you are using now), you can use EMF-based technologies like OCL as well as MDE platforms like Eclipse Epsilon.