274

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 may have errors. Is there any tool out there which could help me with the conversion JSON -> JSON schema?

12
  • 1
    In the example provided, I would say it is clear that we have a dictionary (python terminology), with key-value pairs, where the values happen to be strings. I do not know of any other JSON schema that would describe the same data. And this is just an easy example: it could get much more complicated, of course, as specified in the JSON schema draft. Commented Sep 8, 2011 at 0:18
  • 3
    So you're claiming that "map from arbitrary strings to other arbitrary strings" (such as a mapping from file names to descriptions of the content) cannot be expressed as a JSON schema? For all I know, that may be true, but it would make that kind of schemata rather useless in my view. Commented Sep 8, 2011 at 0:26
  • 1
    Mmmm, I am not sure we are discussing something relevant to the question, but anyway. Let's use a better example: having fixed keys in the JSON data is definitely useful if that JSON data is, for example, describing properties of a person. Instead of "foo" and "bar", think about "name", and "surname". "name" and "surname" are clearly fixed properties of the person JSON data, so they are not arbitrary strings: they are part of the person schema. The values are of course arbitrary, so they are not part of the schema. Commented Sep 8, 2011 at 5:10
  • 3
    Having fixed keys is sometimes what you want, and sometimes it isn't. That's the entire point in fact: there's no way an automated tool can detect from at single sample which of the options you want. Commented Sep 8, 2011 at 11:54
  • 3
    I wouldn't have voted the question off-topic. If you're a programmer, it's a great question. Commented Mar 30, 2020 at 4:28

12 Answers 12

188

Summarising the other answers, here are the JSON schema generators proposed so far:

Online:

Python:

NodeJS:

Ruby:

Sign up to request clarification or add additional context in comments.

6 Comments

jskemetor - no setup.py
Any chance you know if any of these support YAML inputs? We could convert, but just an extra step.
Python: only genson are maintained ^), easy-json-schema works the same as genson and it doesn't have symbols limit like other online tools
liquid-technologies.com/online-json-to-schema-converter -> Did create a exact schema for my input JSON, all existing fields are required - the schema is about 3000 lines easy-json-schema.github.io -> Did simplify a repeating pattern(array), schema is about 70 lines. No fields are required. (But that can be added by adding a * ..)
Just tried genson and it still works great - it's under MIT license and even had some relatively recent updates.
|
101

You might be looking for this:

http://www.jsonschema.net

It is an online tool that can automatically generate JSON schema from JSON string. And you can edit the schema easily.

6 Comments

An easy and handy place to start. But note reported issues with jsonschema.net identified elsewhere on this page, and the reasons discussed for wanting an offline, or at least API-accessible, tool to include in development workflows, allow updating of schemas with later example etc. See also the nice list of options by Steve Bennett.
Please note that this site will throw unexpected errors when editing the schema after the initial import.
Crashes for something like {"hello": "world","num": 42} but looks promising-
The old sites were definitely not good enough. JSONSchema.Net has now been rewritten. It's much more robust. If you have any issues, please report them on GitHub and I'll gladly fix them: github.com/jackwootton/json-schema
Warning - This site now has a login wall unfortunately :(
|
53

GenSON (PyPI | Github) is a JSON Schema generator that can generate a single schema from multiple objects. You can also merge schemas with it. It is written in Python and comes with a CLI tool.

(Full disclosure: I'm the author.)

9 Comments

Nice work, man! I regret not finding this before I started to work on skinfer: github.com/scrapinghub/skinfer
Not a python, but here's another one github.com/snowplow/schema-guru
Great! I've been disappointed with the online schema generator jsonschema.net (it fails to create "required" properties for most objects, has no options to produce compact (one-line) properties or omit IDs, and most importantly, generates a schema that fails to validate the data used to create it for single-schema arrays). Looking forward to trying your tool.
@Dave - i m too facing similar problems with json schema.net, did this python tool help ?
@Cshah: I'm extremely impressed with GenSON and contributed a patch to it. I needed to generate more restrictive schemas than the author was comfortable with so I forked a version with options to generate pattern properties and additionalProperties / additionalItems so that unrecognized JSON data will be flagged as needing attention.
|
21

Seeing that this question is getting quite some upvotes, I add new information (I am not sure if this is new, but I couldn't find it at the time)

Comments

6

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.

3 Comments

Curious as to how @Green Su's suggestion didn't live up to your needs. I think you are describing a utility that provides jumpstarter (your term is 'skeletal') - something like a scaffolding code generator?
Basically, the problem with that tool is that it is an online tool. I need it to run it locally in my development environment, sometimes automatically as part of other tasks. A "copy here, paste there" tool does not help me. If it had a REST API that would be good enough.
@justSteve: the online tool, in addition to using a copy-paste workflow, still appears buggy (4 years after the original question). I have json objects for which the tool produces incorrect schemas but have not yet reduced them to minimal test cases to submit as bug reports.
5

There's a python tool to generate JSON Schema for a given JSON: https://github.com/perenecabuto/json_schema_generator

1 Comment

This is unmaintained since 2013. It doesn't support Python 3. Moreover, it only supports an older draft, i.e. draft-03.
5

generate-schema (NPM | Github) takes a JSON Object generates schemas from it, one output is JSON Schema, it's written in Node.js and comes with a REPL and ClI tool for piping files into.

Full Disclosure: I'm the author :)

1 Comment

Any plans to update the module to draft 4+? Adding min, max attrs, references and so on? Thanks for the tool btw :) Will be using it in my Project
5

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);

1 Comment

Comes with a CLI as well!
3

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.

Comments

3

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 

1 Comment

First, can you provide an answer to unix.stackexchange.com/questions/211803/…?
2

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.

Comments

1

For node.js > 6.0.0 there is also the json-schema-by-example module.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.