Tool to generate JSON schema from JSON data

298,315

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)

Solution 2

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

Online:

Python:

NodeJS:

Ruby:

Solution 3

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.

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.

Share:
298,315

Related videos on Youtube

blueFast
Author by

blueFast

Updated on July 21, 2022

Comments

  • blueFast
    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
      hmakholm left over Monica over 12 years
      But how would the tool know that it is not a generic map from strings to strings?
    • blueFast
      blueFast over 12 years
      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.
    • hmakholm left over Monica
      hmakholm left over Monica over 12 years
      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.
    • blueFast
      blueFast over 12 years
      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.
    • hmakholm left over Monica
      hmakholm left over Monica over 12 years
      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.
    • blueFast
      blueFast over 12 years
      I 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
      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
      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
      Dave about 8 years
      If 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 of properties. I have run across json data like that; the designer used numbers for property names that were arbitrary. And I have an extension to GenSON that generates patternProperties if you give it a regex matching the properties to be treated as arbitrary.
    • Someone Somewhere
      Someone Somewhere about 4 years
      you probably want this jsonschema2pojo.org It's the best to create pojo's from API documentation !
    • Someone Somewhere
      Someone Somewhere about 4 years
      I wouldn't have voted the question off-topic. If you're a programmer, it's a great question.
    • Sailesh Kotha
      Sailesh Kotha over 3 years
      Try this tool, I've been using it for few months debug.center/json-schema-generator
  • Rana Hamza
    Rana Hamza over 11 years
    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?
  • blueFast
    blueFast over 11 years
    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.
  • Alfian Nahar
    Alfian Nahar over 8 years
    Nice work, man! I regret not finding this before I started to work on skinfer: github.com/scrapinghub/skinfer
  • chuwy
    chuwy over 8 years
    Not a python, but here's another one github.com/snowplow/schema-guru
  • Dave
    Dave about 8 years
    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
    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
    Dave about 8 years
    First, can you provide an answer to unix.stackexchange.com/questions/211803/…?
  • Peter Ilfrich
    Peter Ilfrich about 8 years
    Comes with a CLI as well!
  • Cshah
    Cshah about 7 years
    @Dave - i m too facing similar problems with json schema.net, did this python tool help ?
  • Dave
    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
    Asclepius over 6 years
    This is unmaintained since 2013. It doesn't support Python 3. Moreover, it only supports an older draft, i.e. draft-03.
  • Asclepius
    Asclepius over 6 years
    @Dave Why not raise adequate PRs into genson so we can all benefit?
  • Att Righ
    Att Righ over 6 years
    jskemetor - no setup.py
  • nealmcb
    nealmcb over 6 years
    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.
  • Coreus
    Coreus over 6 years
    Please note that this site will throw unexpected errors when editing the schema after the initial import.
  • wolverdude
    wolverdude over 6 years
    patternProperties are now supported
  • DBX12
    DBX12 about 6 years
    Crashes for something like {"hello": "world","num": 42} but looks promising-
  • Jack
    Jack about 6 years
    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
  • Jack
    Jack about 6 years
    As 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
    Mr. Alien over 5 years
    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
  • DylanYoung
    DylanYoung over 4 years
    Any chance you know if any of these support YAML inputs? We could convert, but just an extra step.
  • Someone Somewhere
    Someone Somewhere about 4 years
    jsonschema2pojo.org is what I've been using for years
  • James Madison
    James Madison over 3 years
    Well that was a shockingly smooth experience. Installed easily and did exactly what I needed it to do. Great solution!
  • user2085368
    user2085368 over 2 years
    Warning - This site now has a login wall unfortunately :(