将 JSON 转换为 JSON 与 Swagger 2.0 兼容的架构草案 4
Convert JSON to JSON Schema draft 4 compatible with Swagger 2.0
我得到了一些由 REST API 生成的具有大量属性的 JSON 文件。
我已经为此 API 创建了一个 Swagger 2.0 定义,需要为其提供相应的响应模式。
主要问题:这个 JSON 文件有很多属性。这会花费很多时间,如果我手动编写模式,我会犯很多错误。而且这不是我唯一需要描述的 API。
我知道有一些工具可以将 JSON 转换为 JSON 模式,但是,如果我没记错的话,Swagger 只有 $refs 到其他对象定义,因此只有一个级别,而我发现的工具只能生成树结构的模式。
我的问题:是否有任何工具可以将 JSON(或 JSON 模式)转换为与 Swagger 2.0 兼容的模式?
注意:我在 YAML 中工作,但我不会成为问题,对吗?
比如我需要的:
List of Movements:
type: "array"
items:
$ref: "#/definitions/Movement"
Movement:
properties:
dateKey:
type: "string"
movement:
$ref: "#/definitions/Stock"
additionalProperties: false
Stock:
properties:
stkUnitQty:
type: "string"
stkDateTime:
type: "string"
stkUnitType:
type: "string"
stkOpKey:
type: "string"
additionalProperties: false
对于我的 JSON 文档:
[
{
"dateKey": "20161110",
"stkLvls": [
{
"stkOpKey": "0",
"stkUnitType": "U",
"stkDateTime": "20161110T235010.240+0100",
"stkUnitQty": 30
}
]
},
{
"dateKey": "20161111",
"stkLvls": [
{
"stkOpKey": "0",
"stkUnitType": "U",
"stkDateTime": "20161111T231245.087+0100",
"stkUnitQty": 21
}
]
}
]
但是,http://jsonschema.net/#/ 给我的是:
---
"$schema": http://json-schema.org/draft-04/schema#
type: array
items:
type: object
properties:
dateKey:
type: string
stkLvls:
type: array
items:
type: object
properties:
stkOpKey:
type: string
stkUnitType:
type: string
stkDateTime:
type: string
stkUnitQty:
type: integer
required:
- stkOpKey
- stkUnitType
- stkDateTime
- stkUnitQty
required:
- dateKey
- stkLvls
我是新手,但很好奇,请不要犹豫,深入解释。
提前感谢您的帮助!
I know there are some tools to convert JSON to JSON schemas but, if
I’m not mistaken, Swagger only has $refs to other objects definitions
thus only has one level
你错了。 Swagger 将尊重任何有效的 v4 JSON 模式,只要它只使用 supported subset。
The Schema Object...is based on the JSON Schema Specification Draft 4 and
uses a predefined subset of it. On top of this subset, there are extensions provided by this specification to allow for more complete
documentation.
它接着列出了 JSON 架构中受支持的部分,不支持的部分,以及由 swagger 扩展的部分。
我也需要一个转换器工具,然后遇到了这个。到目前为止,它似乎工作得很好。它支持 JSON 和 YAML 格式。
https://swagger-toolbox.firebaseapp.com/
鉴于此 JSON(他们的样本):
{
"id": 1,
"name": "A green door",
"price": 12,
"testBool": false,
"tags": [
"home",
"green"
]
}
它生成了这个:
{
"required": [
"id",
"name",
"price",
"testBool",
"tags"
],
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number"
},
"testBool": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
您可以直接转到https://bikcrum.github.io/Swagger-JSON-Schema-In-YAML_webversion/进行在线转换。
我编写了以下 python 脚本来生成 JSON 用于 Swagger 的 YAML 格式(保留键顺序)模式。
import json
# input file containing json file
with open('data.json') as f:
json_data = json.load(f)
# json schema in yaml format
out = open('out.yaml','w')
def gettype(type):
for i in ['string','boolean','integer']:
if type in i:
return i
return type
def write(string):
print(string)
out.write(string+'\n')
out.flush()
def parser(json_data,indent):
if type(json_data) is dict:
write(indent + 'type: object')
if len(json_data) > 0:
write(indent + 'properties:')
for key in json_data:
write(indent + ' %s:' % key)
parser(json_data[key], indent+' ')
elif type(json_data) is list:
write(indent + 'type: array')
write(indent + 'items:')
if len(json_data) != 0:
parser(json_data[0], indent+' ')
else:
write(indent + ' type: object')
else:
write(indent + 'type: %s' % gettype(type(json_data).__name__))
parser(json_data,'')
更新:如果您想要带有排序键的 YAML(默认情况下),请使用 YAML 库
import json
import yaml
# input file containing json file
with open('data.json') as f:
json_data = json.load(f)
# json schema in yaml format
def gettype(type):
for i in ['string','boolean','integer']:
if type in i:
return i
return type
def parser(json_data):
d = {}
if type(json_data) is dict:
d['type'] = 'object'
for key in json_data:
d[key] = parser(json_data[key])
return d
elif type(json_data) is list:
d['type'] = 'array'
if len(json_data) != 0:
d['items'] = parser(json_data[0])
else:
d['items'] = 'object'
return d
else:
d['type'] = gettype(type(json_data).__name__)
return d
p = parser(json_data)
with open('out.yaml','w') as outfile:
yaml.dump(p,outfile, default_flow_style=False)
我得到了一些由 REST API 生成的具有大量属性的 JSON 文件。
我已经为此 API 创建了一个 Swagger 2.0 定义,需要为其提供相应的响应模式。
主要问题:这个 JSON 文件有很多属性。这会花费很多时间,如果我手动编写模式,我会犯很多错误。而且这不是我唯一需要描述的 API。
我知道有一些工具可以将 JSON 转换为 JSON 模式,但是,如果我没记错的话,Swagger 只有 $refs 到其他对象定义,因此只有一个级别,而我发现的工具只能生成树结构的模式。 我的问题:是否有任何工具可以将 JSON(或 JSON 模式)转换为与 Swagger 2.0 兼容的模式?
注意:我在 YAML 中工作,但我不会成为问题,对吗?
比如我需要的:
List of Movements:
type: "array"
items:
$ref: "#/definitions/Movement"
Movement:
properties:
dateKey:
type: "string"
movement:
$ref: "#/definitions/Stock"
additionalProperties: false
Stock:
properties:
stkUnitQty:
type: "string"
stkDateTime:
type: "string"
stkUnitType:
type: "string"
stkOpKey:
type: "string"
additionalProperties: false
对于我的 JSON 文档:
[
{
"dateKey": "20161110",
"stkLvls": [
{
"stkOpKey": "0",
"stkUnitType": "U",
"stkDateTime": "20161110T235010.240+0100",
"stkUnitQty": 30
}
]
},
{
"dateKey": "20161111",
"stkLvls": [
{
"stkOpKey": "0",
"stkUnitType": "U",
"stkDateTime": "20161111T231245.087+0100",
"stkUnitQty": 21
}
]
}
]
但是,http://jsonschema.net/#/ 给我的是:
---
"$schema": http://json-schema.org/draft-04/schema#
type: array
items:
type: object
properties:
dateKey:
type: string
stkLvls:
type: array
items:
type: object
properties:
stkOpKey:
type: string
stkUnitType:
type: string
stkDateTime:
type: string
stkUnitQty:
type: integer
required:
- stkOpKey
- stkUnitType
- stkDateTime
- stkUnitQty
required:
- dateKey
- stkLvls
我是新手,但很好奇,请不要犹豫,深入解释。
提前感谢您的帮助!
I know there are some tools to convert JSON to JSON schemas but, if I’m not mistaken, Swagger only has $refs to other objects definitions thus only has one level
你错了。 Swagger 将尊重任何有效的 v4 JSON 模式,只要它只使用 supported subset。
The Schema Object...is based on the JSON Schema Specification Draft 4 and uses a predefined subset of it. On top of this subset, there are extensions provided by this specification to allow for more complete documentation.
它接着列出了 JSON 架构中受支持的部分,不支持的部分,以及由 swagger 扩展的部分。
我也需要一个转换器工具,然后遇到了这个。到目前为止,它似乎工作得很好。它支持 JSON 和 YAML 格式。
https://swagger-toolbox.firebaseapp.com/
鉴于此 JSON(他们的样本):
{
"id": 1,
"name": "A green door",
"price": 12,
"testBool": false,
"tags": [
"home",
"green"
]
}
它生成了这个:
{
"required": [
"id",
"name",
"price",
"testBool",
"tags"
],
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number"
},
"testBool": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
您可以直接转到https://bikcrum.github.io/Swagger-JSON-Schema-In-YAML_webversion/进行在线转换。
我编写了以下 python 脚本来生成 JSON 用于 Swagger 的 YAML 格式(保留键顺序)模式。
import json
# input file containing json file
with open('data.json') as f:
json_data = json.load(f)
# json schema in yaml format
out = open('out.yaml','w')
def gettype(type):
for i in ['string','boolean','integer']:
if type in i:
return i
return type
def write(string):
print(string)
out.write(string+'\n')
out.flush()
def parser(json_data,indent):
if type(json_data) is dict:
write(indent + 'type: object')
if len(json_data) > 0:
write(indent + 'properties:')
for key in json_data:
write(indent + ' %s:' % key)
parser(json_data[key], indent+' ')
elif type(json_data) is list:
write(indent + 'type: array')
write(indent + 'items:')
if len(json_data) != 0:
parser(json_data[0], indent+' ')
else:
write(indent + ' type: object')
else:
write(indent + 'type: %s' % gettype(type(json_data).__name__))
parser(json_data,'')
更新:如果您想要带有排序键的 YAML(默认情况下),请使用 YAML 库
import json
import yaml
# input file containing json file
with open('data.json') as f:
json_data = json.load(f)
# json schema in yaml format
def gettype(type):
for i in ['string','boolean','integer']:
if type in i:
return i
return type
def parser(json_data):
d = {}
if type(json_data) is dict:
d['type'] = 'object'
for key in json_data:
d[key] = parser(json_data[key])
return d
elif type(json_data) is list:
d['type'] = 'array'
if len(json_data) != 0:
d['items'] = parser(json_data[0])
else:
d['items'] = 'object'
return d
else:
d['type'] = gettype(type(json_data).__name__)
return d
p = parser(json_data)
with open('out.yaml','w') as outfile:
yaml.dump(p,outfile, default_flow_style=False)