Zignd
Zignd

Reputation: 7025

How do I verify if JSON data satisfy a JSON Schema specification?

I have JSON data in a file called B.json and another file with a JSON schema called BSchema.json.

I'd like to know how to verify if the JSON data satisfy the JSON Schema specification, for example, in Ubuntu and Windows I can use the xmllint program from the command line to verify the same using the following command: xmllint --schema XMLSchemaFile.xsd --noout DataFile.xml. So, is there any alternative to this command (in Linux or Windows) that allows me to input the two files and check if the JSON data satisfy the JSON Schema?

Note: if there is any other command like that in MacOS, please add to your answer, so the questions will be useful for users of all platforms.

Upvotes: 8

Views: 20752

Answers (4)

Tomasz Chudzik
Tomasz Chudzik

Reputation: 1947

In Powershell Core there is a built-in method that allows you to do the validation test-json. PowerShell syntax: Test-Json [-Json] <String> [[-Schema] <String>]. For more information have a look here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/test-json

In the question it was also asked about the environments. Powershell Core v.7.0+ is cross-platform so this validation can be done anywhere. If you can't or don't want to use PowerShell you can try some Python tool like this: Validate JSON file syntax in shell script without installing any package

Upvotes: 2

rajeshnair
rajeshnair

Reputation: 1673

There are lot of command-line tools available that will allow you to validate a json against json schema

If it is a one-time thing , you can look at json-spec. But I found it lacking in following references in json schema.

But if you need a CI tool and you use node.js/grunt as your dev environment, use tv4 and its grunt job grunt-tv4

Upvotes: 1

cgmb
cgmb

Reputation: 4414

As of version 2.4, jsonschema includes a command-line program to check some input json file against a schema specified in another file.

You can call it like so:

jsonschema -i B.json BSchema.json

Here's some example output from when I put an object where an array should be:

{u'description': u'Doubles resource collection.'}: {u'description': u'Doubles resource collection.'} is not of type u'array'

Upvotes: 9

snollygolly
snollygolly

Reputation: 1886

Here is a web based tool that looks to do what you want it to: http://jsonschemalint.com/

It looks to be based off of this library: https://github.com/garycourt/JSV

I hope this helps!

Upvotes: -2

Related Questions