Tool to generate JSON schema from JSON data
Solution 1
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)
- The home of JSON Schema
- An implementation of JSON Schema validation for Python
- Related hacker news discussion
- A json schema generator in python, which is what I was looking for.
Solution 2
Summarising the other answers, here are the JSON schema generators proposed so far:
Online:
- https://www.liquid-technologies.com/online-json-to-schema-converter (1 input)
- http://www.jsonschema.net (1 input)
- https://easy-json-schema.github.io (1 input)
Python:
- https://github.com/gonvaled/jskemator (1 input but allows iteration)
- https://github.com/perenecabuto/json_schema_generator (1 input)
- https://github.com/rnd0101/json_schema_inferencer (1 input I think)
- https://pypi.python.org/pypi/genson/ (multiple inputs)
- https://pypi.python.org/pypi/skinfer (multiple inputs)
NodeJS:
- https://github.com/Nijikokun/generate-schema (multiple inputs (pass object array))
- https://github.com/easy-json-schema/easy-json-schema (1 input)
- https://github.com/aspecto-io/genson-js (multiple inputs)
Ruby:
Solution 3
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.
Solution 4
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.)
Solution 5
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.
Related videos on Youtube
blueFast
Updated on July 21, 2022Comments
-
blueFast almost 2 years
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?
-
hmakholm left over Monica over 12 yearsBut how would the tool know that it is not a generic map from strings to strings?
-
blueFast over 12 yearsIn 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.
-
hmakholm left over Monica over 12 yearsSo 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.
-
blueFast over 12 yearsMmmm, 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.
-
hmakholm left over Monica over 12 yearsHaving 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.
-
blueFast over 12 yearsI see what you mean. Let's say all key names are considered by default being fixed: a tool could work that way. Then it would produce the skeleton of the JSON schema, using the data types inferred from the JSON data. Most of the information would be of course arbitrary (the tool can not know about most things - is it required, what is the description?), but I would still find value in having the skeleton produced for me, filled with dummy values, even if I have to edit it heavily.
-
Dave about 8 years@HenningMakholm, a set of arbitrary pairs of strings (such as filename: description) would more logically be expressed as a list than a mapping:
{"type":"array","items":{["string","string"]}}
. I would say that fixed keys are nearly always what is intended with objects - the very word "properties" carries with it the implication that a property has a given name and a value with property-specific syntax. -
hmakholm left over Monica about 8 years@Dave: That doesn't seem to be the case in the schema language being employed here, though.
-
Dave about 8 yearsIf you know for a fact that an object is being used to carry arbitrary pairs, then you can tell the schema generator to use
patternProperties
instead ofproperties
. I have run across json data like that; the designer used numbers for property names that were arbitrary. And I have an extension toGenSON
that generatespatternProperties
if you give it a regex matching the properties to be treated as arbitrary. -
Someone Somewhere about 4 yearsyou probably want this jsonschema2pojo.org It's the best to create pojo's from API documentation !
-
Someone Somewhere about 4 yearsI wouldn't have voted the question off-topic. If you're a programmer, it's a great question.
-
Sailesh Kotha over 3 yearsTry this tool, I've been using it for few months debug.center/json-schema-generator
-
-
Rana Hamza over 11 yearsCurious 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?
-
blueFast over 11 yearsBasically, 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.
-
Alfian Nahar over 8 yearsNice work, man! I regret not finding this before I started to work on skinfer: github.com/scrapinghub/skinfer
-
chuwy over 8 yearsNot a python, but here's another one github.com/snowplow/schema-guru
-
Dave about 8 yearsGreat! 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 about 8 years@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.
-
Dave about 8 yearsFirst, can you provide an answer to unix.stackexchange.com/questions/211803/…?
-
Peter Ilfrich about 8 yearsComes with a CLI as well!
-
Cshah about 7 years@Dave - i m too facing similar problems with json schema.net, did this python tool help ?
-
Dave about 7 years@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.
-
Asclepius over 6 yearsThis is unmaintained since 2013. It doesn't support Python 3. Moreover, it only supports an older draft, i.e.
draft-03
. -
Asclepius over 6 years@Dave Why not raise adequate PRs into genson so we can all benefit?
-
Att Righ over 6 yearsjskemetor - no
setup.py
-
nealmcb over 6 yearsAn 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.
-
Coreus over 6 yearsPlease note that this site will throw unexpected errors when editing the schema after the initial import.
-
wolverdude over 6 yearspatternProperties are now supported
-
DBX12 about 6 yearsCrashes for something like
{"hello": "world","num": 42}
but looks promising- -
Jack about 6 yearsThe 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
-
Jack about 6 yearsAs mentioned elsewhere, 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
-
Mr. Alien over 5 yearsAny 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
-
DylanYoung over 4 yearsAny chance you know if any of these support YAML inputs? We could convert, but just an extra step.
-
Someone Somewhere about 4 yearsjsonschema2pojo.org is what I've been using for years
-
James Madison over 3 yearsWell that was a shockingly smooth experience. Installed easily and did exactly what I needed it to do. Great solution!
-
user2085368 over 2 yearsWarning - This site now has a login wall unfortunately :(