gpt4 book ai didi

python - 如何在 python 中通过 jsonschema 过滤 json

转载 作者:行者123 更新时间:2023-12-05 07:47:02 29 4
gpt4 key购买 nike

我在 Python 中有一个 json 文件和一个相关的 json-schema,我想从输入 json 文件中过滤掉所有不满足相对 json 模式的元素。

例如:

架构

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":false,
"properties":{
"address": {
"type":"object",
"required":true,
"properties":{
"city": {
"type":"string",
"required":true
},
"houseNumber": {
"type":"number",
"required":false
},
"streetAddress": {
"type":"string",
"required":true
}
}
},
"phoneNumber": {
"type":"array",
"required":false,
"items":
{
"type":"object",
"required":false,
"properties":{
"number": {
"type":"string",
"required":false
},
"type": {
"type":"string",
"required":false
}
}
}
}
}
}

Json 输入:

{
"address":{
"streetAddress": "21 2nd Street",
"city":"New York",
"houseNumber":12
},
"phoneNumber":
[
{
"type":"home",
"number":"212 555-1234",
"description":"blabla"
}
]
}

元素 "description":"blabla" 不存在于 json 架构中,必须进行过滤。

Json 输出:

{
"address":{
"streetAddress": "21 2nd Street",
"city":"New York",
"houseNumber":12
},
"phoneNumber":
[
{
"type":"home",
"number":"212 555-1234"
}
]
}

我找到了这个 https://github.com/alank64/json-schema-filter但它适用于 javascript。您是否知道任何能够在 Python 中执行此操作的脚本?

最佳答案

关于python - 如何在 python 中通过 jsonschema 过滤 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40226596/

29 4 0
文章推荐: visual-studio-2015 - 如何在 ASP.NET Core MVC 中添加 DefinitelyTyped
文章推荐: javascript - Material -UI + React : Why doesn't 's onRowSelection work for Select All?