【问题标题】:Create JSON instance from JSON schema从 JSON 模式创建 JSON 实例
【发布时间】:2019-10-04 06:45:40
【问题描述】:

您好,我正在尝试从 JSON 架构创建 JSON 实例。

欢迎打字稿回答,但感谢任何答案。

在 sample-schema.json 文件下方。我想创建一个函数来将模式转换为模式实例。

例如:sample-schema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "address": {
      "type": "object",
      "properties": {
        "street_1": {
          "type": "string"
        },
        "street_2": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "type": "string",
          "enum": [
            "AL",
            "AK",
            "AS"
          ]
        },
        "zip_code": {
          "type": "string"
        }
      }
    },
    "birthday": {
      "type": "string"
    },
    "notes": {
      "type": "string"
    }
  },
  "required": [
    "last_name"
  ]
}

期望的输出:

{
  "first_name": "",
  "last_name": "",
  "last_name": "",
  "address": {
    "street_1": "",
    "street_2": "",
    "city": "",
    "state": "",
    "zip_code": ""
  },
  "birthday": "",
  "notes": ""
}

在调用函数时,通过传递它必须生成 JSON 实例的模式。我在 Google 上搜索,但所有结果都是从 JSON 到 JSON 模式。

【问题讨论】:

  • 粗略浏览一下互联网似乎表明,如果您想通过 JavaScript 编写一些东西,您可能需要自己编写一些东西。我找到了一个可以做到这一点的 Java 工具,但没有你可以自己引入的 JavaScript 库。
  • 如果您有任何来源,请提供给我。任何语言都会尝试修改为我的风格。

标签: javascript typescript jsonschema


【解决方案1】:

我从这个json-schema-filter找到了javascript

【讨论】:

    【解决方案2】:

    获取sampleSchema 的值并使用javascript JSON.parse(sampleSchema) 方法。

    【讨论】:

    • 想详细说明一下?您的回答没有提到它指的是哪个库或框架。
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 2016-08-09
    • 2017-01-14
    • 2017-09-27
    • 2021-12-11
    相关资源
    最近更新 更多