使用 Java 中的 JSON 模式定义从 JSON 中删除可选元素

问题描述 投票:0回答:1

我们有一个需求,通过队列传递JSON字符串,队列有大小限制。是否有可能使用 JSON 模式从 JSON 字符串/对象中删除可选元素,以便它可以放入队列中。

假设以下是一个示例 JSON Schema:

{
  "components": {
    "schemas": {
      "Data": {
        "properties": {
          "admittedOn": {
            "example": "2023-02-20",
            "title": "The admittedOn Schema",
            "type": "string"
          },
          "claimAmount": {
            "example": 500,
            "title": "The claimAmount Schema",
            "type": "integer"
          },
          "clinicName": {
            "enum": [
              "Mayo Clinic",
              "Appollo",
              "Care",
              "Govt"
            ],
            "example": "Mayo Clinic",
            "title": "The clinicName Schema",
            "type": "string"
          },
          "dischargedOn": {
            "example": "2023-02-25",
            "title": "The dischargedOn Schema",
            "type": "string"
          },
          "firstName": {
            "example": "Jon",
            "title": "The firstName Schema",
            "type": "string"
          },
          "lastName": {
            "example": "Doe",
            "title": "The lastName Schema",
            "type": "string"
          },
          "stateCode": {
            "example": "MN",
            "title": "The state code",
            "type": "string"
          }
        },
        "required": [
          "firstName",
          "lastName",
          "admittedOn",
          "clinicName"
        ],
        "type": "object"
      },
      "Event": {
        "additionalProperties": false,
        "properties": {
          "data": {
            "$ref": "#/components/schemas/Data"
          },
          "datacontenttype": {
            "type": "string"
          },
          "dataref": {
            "type": "string"
          },
          "dataschema": {
            "type": "string"
          },
          "environment": {
            "pattern": "^[^pP]|^.[^rR]|^..[^oO]|^...[^dD]$",
            "type": "string"
          },
          "id": {
            "type": "string"
          },
          "partitionkey": {
            "type": "string"
          },
          "sampledrate": {
            "type": "integer"
          },
          "sequence": {
            "type": "string"
          },
          "severitynumber": {
            "type": "integer"
          },
          "severitytext": {
            "type": "string"
          },
          "source": {
            "pattern": "^[a-zA-Z0-9\\.\\_\\-\\:\\@\\/]*$",
            "type": "string"
          },
          "specversion": {
            "enum": [
              "1.0"
            ],
            "type": "string"
          },
          "subject": {
            "type": "string"
          },
          "time": {
            "pattern": "^((?:(\\d{4}-\\d{2}-\\d{2})T(\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?))(Z|[\\+-]\\d{2}:\\d{2})?)$",
            "type": "string"
          },
          "traceparent": {
            "pattern": "^[\\da-f]{2}-[\\da-f]{32}-[\\da-f]{16}-[\\da-f]{2}$",
            "type": "string"
          },
          "tracestate": {
            "pattern": "^([^,]+=[^,]+)(,([^,]+=[^,]+))*?$",
            "type": "string"
          },
          "type": {
            "default": "patient.covid.claims.v3.0.0",
            "enum": [
              "patient.covid.claims.v3.0.0"
            ],
            "pattern": "^[a-zA-Z0-9\\.\\_\\-]*$",
            "type": "string"
          }
        },
        "required": [
          "data",
          "environment",
          "id",
          "source",
          "specversion",
          "type"
        ],
        "type": "object"
      }
    }
  }
}

示例 JSON 字符串:

{
  "id": "5a54e6ae-b92e-4393-a360-e28376fa7af5",
  "type": "patient.covid.claims.v1.0.0",
  "source": "myapp",
  "specversion": "1.0",
  "subject": "Claim request",
  "time": "2023-11-06T17:40:18.003Z",
  "datacontenttype": "application/json",
  "environment": "dev",
  "data": {
    "firstName": "Myrtie Bowman",
    "lastName": "Ethel Harper",
    "admittedOn": "2023-02-20",
    "dischargedOn": "2023-02-25",
    "clinicName": "Mayo Clinic",
    "claimAmount": 500,
    "stateCode": "MN"
  }
}

删除可选值后,预期的 JSON 字符串如下所示:

{
  "id": "5a54e6ae-b92e-4393-a360-e28376fa7af5",
  "type": "patient.covid.claims.v1.0.0",
  "source": "myapp",
  "specversion": "1.0",
  "environment": "dev",
  "data": {
    "firstName": "Myrtie Bowman",
    "lastName": "Ethel Harper",
    "admittedOn": "2023-02-20",
    "clinicName": "Mayo Clinic"
  }
}

关于如何使用模式定义从 Java 中的 JSON 中删除可选元素有什么想法吗?

java jsonschema json-schema-validator
1个回答
0
投票

使用 JSON Schema 从 JSON 中删除可选元素...

JSON Schema 不是一个转换工具。它验证并注释。

虽然该输出可以“使用”来转换 JSON,但您可能需要自己编写该代码。您可能会在 https://json-schema.org/implementations 上找到一个可以提供帮助的工具。

就个人而言,我会查看
required

关键字并手动(即编写代码)从实例中删除未列出的属性。

    

© www.soinside.com 2019 - 2024. All rights reserved.