我们有这个json模式草案。我想获得我的JSON数据的样本,并为JSON模式生成一个框架,我可以手动返工,添加像描述,要求等东西,这不能从具体的例子推断。

例如,在我的输入example.json中:

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

我将运行我的json_schema_generator工具,并将得到:

{ "foo": {
    "type" : "string",
    "required" : true,
    "description" : "unknown"
  },
  "bar": {
    "type" : "string",
    "required" : true,
    "description" : "unknown"
  }
}

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


当前回答

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 

其他回答

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

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

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

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

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

schemaObj = jsonSchemaGenerator(json);

有一个python工具可以为给定的JSON生成JSON Schema: https://github.com/perenecabuto/json_schema_generator

对于node.js > 6.0.0,也有json-schema-by-example模块。

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

JSON模式的家 Python JSON模式验证的实现 相关黑客新闻讨论 python中的json模式生成器,这就是我正在寻找的。