从 JSON 数据生成 JSON 模式的工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7341537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:58:42  来源:igfitidea点击:

Tool to generate JSON schema from JSON data

jsonvalidationreflectionjsonschema

提问by blueFast

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.

我们有这个 json 模式草案。我想获取我的 JSON 数据样本并为 JSON 模式生成一个骨架,我可以手动修改它,添加诸如描述、必需等内容,这些内容无法从具体示例中推断出来。

For example, from my input example.json:

例如,从我的输入example.json

{
    "foo": "lorem", 
    "bar": "ipsum"
}

I would run my json_schema_generator tool and would get:

我会运行我的 json_schema_g​​enerator 工具并得到:

{ "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?

这个例子是手动编码的,所以它可能有错误。是否有任何工具可以帮助我转换 JSON -> JSON 模式?

采纳答案by blueFast

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)

看到这个问题得到了相当多的赞成,我添加了新信息(我不确定这是不是新信息,但当时我找不到)

回答by Green Su

You might be looking for this:

你可能正在寻找这个:

http://www.jsonschema.net

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.

它是一个在线工具,可以从 JSON 字符串自动生成 JSON 模式。您可以轻松编辑架构。

回答by Steve Bennett

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

总结其他答案,以下是迄今为止提出的 JSON 模式生成器:

Online:

在线的:

Python:

Python:

NodeJS:

节点:

Ruby:

红宝石:

回答by wolverdude

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.

GenSON ( PyPI| Github) 是一个 JSON 模式生成器,可以从多个对象生成单个模式。您还可以将模式与其合并。它是用 Python 编写的,并带有一个 CLI 工具。

(Full disclosure: I'm the author.)

(完全披露:我是作者。)

回答by blueFast

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.

我想要的是类似的东西这个。JSON 数据可以为 JSON 模式提供框架。我还没有实现它,但应该可以给出一个现有的 JSON 模式作为基础,这样现有的 JSON 模式加上 JSON 数据可以生成更新的 JSON 模式。如果没有给出这样的模式作为输入,则采用完全默认值。

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.

这在迭代开发中非常有用:第一次运行该工具时,JSON 模式是虚拟的,但可以根据数据的演变自动细化。

回答by Vinícius Mendes

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

有一个 python 工具可以为给定的 JSON 生成 JSON 模式:https: //github.com/perenecabuto/json_schema_g​​enerator

回答by Nijikokun

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.

generate-schema( NPM| Github) 接受一个 JSON 对象,从中生成模式,一个输出是 JSON 模式,它是用 Node.js 编写的,并带有一个 REPL 和 ClI 工具,用于将文件导入。

Full Disclosure: I'm the author :)

完全披露:我是作者:)

回答by Johann Philipp Strathausen

There's a nodejs tool which supports json schema v4 at https://github.com/krg7880/json-schema-generator

https://github.com/krg7880/json-schema-generator有一个支持 json schema v4 的 nodejs 工具

It works either as a command line tool, or as a nodejs library:

它既可以用作命令行工具,也可以用作 nodejs 库:

var jsonSchemaGenerator = require('json-schema-generator'),
    obj = { some: { object: true } },
    schemaObj;

schemaObj = jsonSchemaGenerator(json);

回答by John Carlson

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 classesfrom 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.

对于支持多输入的离线工具,到目前为止我见过的最好的是https://github.com/wolverdude/GenSON/我希望看到一个在标准输入上使用文件名的工具,因为我有数千个文件. 但是,我用完了打开的文件描述符,因此请确保文件已关闭。我还希望看到处理递归的 JSON Schema 生成器。我现在正在从 JSON 对象生成 Java,希望能从我的 Java 类转到 JSON Schema。如果您好奇或想识别其中的错误,这是我的 GenSON 脚本。

#!/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 

回答by HappyCoder86

json-schema-generatoris 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.

json-schema-generator是一个简洁的基于 Ruby 的 JSON 模式生成器。它支持 JSON 模式的草案 3 和草案 4。它可以作为独立的可执行文件运行,也可以嵌入到 Ruby 脚本中。

Then you can use json-schemato validate JSON samples against your newly generated schema if you want.

然后,如果需要,您可以使用json-schema根据新生成的模式验证 JSON 样本。