如何在 JSON 中编写“anyOf”语句?

在 JSON 中,anyOf 语句通常用于 JSON Schema(JSON 模式)中,来定义多个可能的模式,表示数据可以匹配多个子模式中的任意一个。这种功能常用于验证 JSON 数据是否符合某一组可能的条件之一。

1、问题背景

问题:如何编写 JSON 使其符合给定的 JSON Schema 结构?在 JSON Schema 中,存在一个"anyOf"关键字,要求至少满足一个条件。

2、解决方案

为了符合给定的 JSON Schema 结构,需要对 JSON 进行以下修改:

  • 使用anyOf关键字可以确保至少满足一个条件。
python 复制代码
special_needs = [
    {
        "main": ["learning"],
        "sub": ["ADD/ADHD"]
    },
    {
        "main": ["behavioral"]
    }
]
  • mainsub属性需要放在同一个对象中。
python 复制代码
special_needs = [
    {
        "category_0": {
            "main": ["learning"],
            "sub": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]
        }
    },
    {
        "category_1": {
            "main": ["mental"],
            "sub": ["down's syndrome", "asperger's syndrome", "autism", "other"]
        }
    },
    {
        "category_2": {
            "main": ["behavioral"]
        }
    },
    {
        "category_3": {
            "main": ["medical"],
            "sub": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]
        }
    },
    {
        "category_4": {
            "main": ["physical"],
            "sub": ["blind", "deaf", "cerebral palsy", "other"]
        }
    }
]
  • 将JSON中的dict改为[],这样才符合anyOf中的要求。
python 复制代码
special_needs = [
    {
        "category_0": {
            "main": ["learning"],
            "sub": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]
        }
    },
    {
        "category_1": {
            "main": ["mental"],
            "sub": ["down's syndrome", "asperger's syndrome", "autism", "other"]
        }
    },
    {
        "category_2": {
            "main": ["behavioral"]
        }
    },
    {
        "category_3": {
            "main": ["medical"],
            "sub": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]
        }
    },
    {
        "category_4": {
            "main": ["physical"],
            "sub": ["blind", "deaf", "cerebral palsy", "other"]
        }
    }
]
  • anyOf仅适用于数组类型,因此special_needs应该是一个数组。
python 复制代码
special_needs = [
    {
        "category_0": {
            "main": ["learning"],
            "sub": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]
        }
    },
    {
        "category_1": {
            "main": ["mental"],
            "sub": ["down's syndrome", "asperger's syndrome", "autism", "other"]
        }
    },
    {
        "category_2": {
            "main": ["behavioral"]
        }
    },
    {
        "category_3": {
            "main": ["medical"],
            "sub": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]
        }
    },
    {
        "category_4": {
            "main": ["physical"],
            "sub": ["blind", "deaf", "cerebral palsy", "other"]
        }
    }
]

代码示例:

python 复制代码
import json

# 创建一个包含"anyOf"关键字的 JSON Schema
schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "special_needs": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "properties": {
                            "category_0": {
                                "type": "object",
                                "properties": {
                                    "main": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["learning"]
                                        }
                                    },
                                    "sub": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["ADD/ADHD", "dyslexia", "general learning disability", "language disorder", "intellectual giftedness", "other"]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    {
                        "properties": {
                            "category_1": {
                                "type": "object",
                                "properties": {
                                    "main": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["mental"]
                                        }
                                    },
                                    "sub": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["down's syndrome", "asperger's syndrome", "autism", "other"]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    {
                        "properties": {
                            "category_2": {
                                "type": "object",
                                "properties": {
                                    "main": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["behavioral"]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    {
                        "properties": {
                            "category_3": {
                                "type": "object",
                                "properties": {
                                    "main": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["medical"]
                                        }
                                    },
                                    "sub": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["diabetes", "allergies", "eating disorders", "chronic illness", "other"]
                                        }
                                    }
                                }
                            }
                        }
                    },
                    {
                        "properties": {
                            "category_4": {
                                "type": "object",
                                "properties": {
                                    "main": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["physical"]
                                        }
                                    },
                                    "sub": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": ["blind", "deaf", "cerebral palsy", "other"]
                                        }
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        }
    }
}

# 创建一个符合 JSON Schema 的 JSON 数据
data = {
    "special_needs": [
        {
            "category_0": {
                "main": ["learning"],
                "sub": ["ADD/ADHD"]
            }
        },
        {
            "category_1": {
                "main": ["mental"],
                "sub": ["down's syndrome"]
            }
        }
    ]
}

# 验证 JSON 数据是否符合 JSON Schema
validator = jsonschema.Draft4Validator(schema)
if validator.is_valid(data):
    print("JSON data is valid")
else:
    print("JSON data is invalid")

# 输出 JSON 数据
print(json.dumps(data, indent=4))

运行代码输出如下:

python 复制代码
JSON data is valid
{
    "special_needs": [
        {
            "category_0": {
                "main": [
                    "learning"
                ],
                "sub": [
                    "ADD/ADHD"
                ]
            }
        },
        {
            "category_1": {
                "main": [
                    "mental"
                ],
                "sub": [
                    "down's syndrome"
                ]
            }
        }
    ]
}

总结

  • anyOf 允许定义多个可能的模式,数据只需符合其中之一即可。
  • 它在 JSON Schema 中用于灵活的验证场景,尤其当字段可以有多种可能的结构时。

这种模式非常适合需要灵活数据验证的场景,比如 API 请求的验证、表单数据的校验等。

相关推荐
艾莉丝努力练剑6 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
武子康1 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
人生游戏牛马NPC1号2 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
StarRocks_labs2 小时前
StarRocks Community Monthly Newsletter (Jun)
数据库·starrocks·数据湖·物化视图·存算分离
光电的一只菜鸡3 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
椰椰椰耶3 小时前
【Spring】拦截器详解
java·后端·spring
ob熔天使——武3 小时前
MySQL
数据库·mysql
小馬佩德罗3 小时前
Android系统的问题分析笔记 - Android上的调试方式 debuggerd
android·调试
没有bug.的程序员4 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis