当前位置: 首页 > news >正文

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

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

在这里插入图片描述

1、问题背景

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

2、解决方案

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

  • 使用anyOf关键字可以确保至少满足一个条件。
special_needs = [{"main": ["learning"],"sub": ["ADD/ADHD"]},{"main": ["behavioral"]}
]
  • mainsub属性需要放在同一个对象中。
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中的要求。
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应该是一个数组。
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"]}}
]

代码示例:

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))

运行代码输出如下:

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 请求的验证、表单数据的校验等。

http://www.lryc.cn/news/461981.html

相关文章:

  • python开发环境配置
  • QT开发--QT SQL模块
  • 如何保证接口幂等性?
  • 【9718】基于springboot+vue的生鲜交易系统
  • Spring循环依赖解决方案
  • 解决 IntelliJ IDEA 运行时 “Command line is too long“ 问题
  • 鸿蒙网络编程系列5-TCP连接超时分析
  • 金蝶云星空移动字段后关闭页面后重新打开无效
  • 幂律分布笔记
  • 一些NLP代表性模型
  • 低代码移动端开发:未来的趋势与挑战
  • 【Linux】嵌入式Linux系统的组成、u-boot编译
  • Qt打开excel文件,并读取指定单元格数据
  • 适合下班回家做的小副业,用AI做视频,几天时间3000+
  • git的基本操作 + 分支管理
  • VRRP
  • 个人健康系统|个人健康数据管理系统|基于小程序+java的个人健康数据管理系统设计与实现(源码+数据库+文档)
  • R语言统计分析——折线图
  • 前端怎么实现电子签名
  • 数字后端零基础入门系列 | Innovus零基础LAB学习Day1
  • 鼠标移入盒子,盒子跟随鼠标移动
  • css的简单问题
  • 使⽤ Override 和 New 关键字进⾏版本控制(C#)
  • JavaScript 15章:模块化编程
  • qt creator 开发环境的安装
  • Xilinx远程固件升级(二)——STARTUPE2原语的使用
  • DynamicExpresso
  • 从Naive RAG到Agentic RAG:基于Milvus构建Agentic RAG
  • Linux 环境chrony设置服务器间时间同步一致
  • MetaCTO确认将放弃QuestPro2及轻量化头显正在开发中