【问题标题】:Get available attributes (possibly recursive) from JSON Schema in Java从 Java 中的 JSON Schema 获取可用属性(可能是递归的)
【发布时间】:2021-08-20 11:45:24
【问题描述】:

假设我有以下 JSON 架构:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Draft JSON Schema",
  "type": "object",
  "properties": {
      "person": {
          "type": "object",
          "properties": {
              "details": {
                  "type": "object",
                  "properties": {
                      "first_name": {
                          "type": "string"
                      },
                      "last_name": {
                          "type": "string"
                      },
                      "groups": {
                          "type": "array",
                          "items": { "$ref": "#/$defs/existing_groups"
                      }
                  }

              }
          }
      },
      "$defs": {
          "existing_groups": [ "Teachers", "Students" ]
      },
      "book": {
          "type": "object",
          "properties": {
              "title": {
                  "type": "string"
              },
              "author": {
                  "type": "string"
              }
          }
      }
  }
}

从这个架构中,我想在定义的深度检索可用的属性和值:

  • 所以给出的是例如person.details 我希望 first_name, last_name, groups 被退回。
  • 如果给定person.details.groups,则应返回可能的值Student, Teacher。
  • 如果给定book.title,则应返回一个空的Array 或Set。

显然您可以使用JsonPath 从JSON 对象中获取属性值,但我更希望从com.networknt.schema.JsonSchema 中获取可能的属性(以及它们的可能值,如果有的话)。

在 Java 中最简单的方法是什么?

【问题讨论】:

    标签: java json jsonschema


    【解决方案1】:

    JSON Schema 用于验证数据。它与数据操作或提取无关。它在任何方面都无法与 JSONPath 相提并论。

    【讨论】:

    • 我刚刚提到 JSONPath 是为了说明我正在为模式寻找类似的东西。我知道 JSON 模式与 JSON 不同。就我而言,我正在使用 JSON 模式,我需要提取根据该模式允许的属性。
    猜你喜欢
    • 2012-12-24
    • 2021-10-25
    • 1970-01-01
    • 2011-05-15
    • 2020-05-25
    • 2014-09-07
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多