使用 jq 从 Swagger JSON 模式中提取对象属性
Extracting objects attributes from Swagger JSON schema using jq
我想从 swagger json 文件的 .definitions
部分提取对象名称及其属性 (.properties
),例如:
{
"definitions": {
"Pet": {
"type": "object",
"required": ["pet_type"],
"properties": {
"pet_type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "pet_type"
}
},
"Dog": {
"allOf": [{
"$ref": "#/components/schemas/Pet"
}, {
"type": "object",
"properties": {
"bark": {
"type": "boolean"
},
"breed": {
"type": "string",
"enum": ["Dingo", "Husky", "Retriever", "Shepherd"]
}
}
}
]
}
}
}
至:
object_name:
attribute_name
喜欢:
Pet:
pet_type
Dog:
bark
breed
重点是 .properties
有时直接跟在对象名称之后,有时作为 allOf
、anyOf
或 oneOf
的一部分
这符合您的需求吗?
jq -r '
.definitions
| to_entries[]
| .key + ":", " " + (
.value
| (., .allOf[], .anyOf[], .oneOf[] | .properties)? // {}
| to_entries[].key
)
'
Pet:
pet_type
Dog:
bark
breed
我想从 swagger json 文件的 .definitions
部分提取对象名称及其属性 (.properties
),例如:
{
"definitions": {
"Pet": {
"type": "object",
"required": ["pet_type"],
"properties": {
"pet_type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "pet_type"
}
},
"Dog": {
"allOf": [{
"$ref": "#/components/schemas/Pet"
}, {
"type": "object",
"properties": {
"bark": {
"type": "boolean"
},
"breed": {
"type": "string",
"enum": ["Dingo", "Husky", "Retriever", "Shepherd"]
}
}
}
]
}
}
}
至:
object_name:
attribute_name
喜欢:
Pet:
pet_type
Dog:
bark
breed
重点是 .properties
有时直接跟在对象名称之后,有时作为 allOf
、anyOf
或 oneOf
这符合您的需求吗?
jq -r '
.definitions
| to_entries[]
| .key + ":", " " + (
.value
| (., .allOf[], .anyOf[], .oneOf[] | .properties)? // {}
| to_entries[].key
)
'
Pet:
pet_type
Dog:
bark
breed