We have this json schema draft. I would like to get a sample of my JSON data and generate a skeleton for the JSON schema, that I can rework manually, adding things like description, required, etc, which can not be infered from the specific examples.
For example, from my input example.json
:
{
"foo": "lorem",
"bar": "ipsum"
}
I would run my json_schema_generator tool and would get:
{ "foo": {
"type" : "string",
"required" : true,
"description" : "unknown"
},
"bar": {
"type" : "string",
"required" : true,
"description" : "unknown"
}
}
This example has been coded manually, so it has maybe errors. Is there any tool out there which could help me with the conversion JSON -> JSON schema?
This question is related to
json
validation
reflection
jsonschema
There are a lot of tools mentioned, but one more called JSON Schema inferencer for the record:
https://github.com/rnd0101/json_schema_inferencer
(it's not a library or a product, but a Python script)
With the usual Full Disclosure: I am the author.
For the offline tools that support multiple inputs, the best I've seen so far is https://github.com/wolverdude/GenSON/ I'd like to see a tool that takes filenames on standard input because I have thousands of files. However, I run out of open file descriptors, so make sure the files are closed. I'd also like to see JSON Schema generators that handle recursion. I am now working on generating Java classes from JSON objects in hopes of going to JSON Schema from my Java classes. Here is my GenSON script if you are curious or want to identify bugs in it.
#!/bin/sh
ulimit -n 4096
rm x3d*json
cat /dev/null > x3d.json
find ~/Downloads/www.web3d.org/x3d/content/examples -name '*json' - print| xargs node goodJSON.js | xargs python bin/genson.py -i 2 -s x3d.json >> x3d.json
split -p '^{' x3d.json x3d.json
python bin/genson.py -i 2 -s x3d.jsonaa -s x3d.jsonab /Users/johncarlson/Downloads/www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter02-GeometryPrimitives/Box.json > x3dmerge.json
There's a python tool to generate JSON Schema for a given JSON: https://github.com/perenecabuto/json_schema_generator
After several months, the best answer I have is my simple tool. It is raw but functional.
What I want is something similar to this. The JSON data can provide a skeleton for the JSON schema. I have not implemented it yet, but it should be possible to give an existing JSON schema as basis, so that the existing JSON schema plus JSON data can generate an updated JSON schema. If no such schema is given as input, completely default values are taken.
This would be very useful in iterative development: the first time the tool is run, the JSON schema is dummy, but it can be refined automatically according to the evolution of the data.
You might be looking for this:
It is an online tool that can automatically generate JSON schema from JSON string. And you can edit the schema easily.
For node.js > 6.0.0 there is also the json-schema-by-example module.
There's a nodejs tool which supports json schema v4 at https://github.com/krg7880/json-schema-generator
It works either as a command line tool, or as a nodejs library:
var jsonSchemaGenerator = require('json-schema-generator'),
obj = { some: { object: true } },
schemaObj;
schemaObj = jsonSchemaGenerator(json);
json-schema-generator is a neat Ruby based JSON schema generator. It supports both draft 3 and 4 of the JSON schema. It can be run as a standalone executable, or it can be embedded inside of a Ruby script.
Then you can use json-schema to validate JSON samples against your newly generated schema if you want.
Summarising the other answers, here are the JSON schema generators proposed so far:
Online:
Python:
NodeJS:
Ruby:
Source: Stackoverflow.com