javascript - Creează o instanță JSON din schema JSON


Bună, încerc să creez o instanță JSON dintr-un schemă JSON.

Un răspuns în Typescript este apreciat, dar apreciez orice răspuns.

Mai jos se află fișierul sample-schema.json. Vreau să creez o funcție pentru a converti un schema într-o instanță de schema.

Ex: 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"
  ]
}

Rezultatul dorit:

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

La apelarea funcției, trecând schema, aceasta trebuie să genereze instanța JSON. Am căutat pe Google, dar toate rezultatele sunt din JSON spre schema JSON.

Răspuns #1

Am găsit javascriptul din acest json-schema-filter

Răspunsul #2

preia valoarea sampleSchema și folosește metoda JavaScript JSON.parse(sampleSchema).