Search code examples
jsonschemajsonschemaoxygenxml

How to make a JSON Schema for a JSON file in Oxygen


I have a Json file and I need to write a Scheme for it in Oxygen.

      "characters": [
        {
          "house":"Gryffindor",
          "orderOfThePhoenix":false,
          "name":"Cuthbert Binns",
          "bloodStatus":"unknown",
          "deathEater":false,
          "dumbledoresArmy":false,
          "school":"Hogwarts School of Witchcraft and Wizardry",
          "role":"Professor, History of Magic",
          "__v":0,
          "ministryOfMagic":false,
          "_id":"5a0fa67dae5bc100213c2333",
          "species":"ghost"
         }
    ],

        "spells": [
             {
              "spell":"Aberto",
              "effect":"opens objects",
              "_id":"5b74ebd5fb6fc0739646754c",
              "type":"Charm"
             }
],
"houses": [
     {
      "values": [
        "courage",
        "bravery",
        "nerve",
        "chivalry"
       ],
      "headOfHouse":"Minerva McGonagall",
      "mascot":"lion",
      "name":"Gryffindor",
      "houseGhost":"Nearly Headless Nick",
      "founder":"Goderic Gryffindor",
      "colors": [
        "scarlet",
        "gold"
       ],
      "school":"Hogwarts School of Witchcraft and Wizardry",
      "__v":0,
      "members": [
        "5a0fa648ae5bc100213c2332",
        "5a0fa67dae5bc100213c2333",
        "5a0fa7dcae5bc100213c2338",
        "5a123f130f5ae10021650dcc"
       ],
      "_id":"5a05e2b252f721a3cf2ea33f"
     },

For sure the current JSON file is much bigger. If someone could send related links it would help too, or some kind of tutorials. Could you please help me with creating a schema for it?


Solution

  • If you want to create a JSON Schema, the best way to start is to check the "json-schema.org" tutorials. You can find them here:

    https://json-schema.org/learn/getting-started-step-by-step.html

    https://json-schema.org/understanding-json-schema/

    In the next version of Oxygen there will be support to create a JSON Schema based on a JSON instance or on an XSD, but you will need to check the created schema and customize it for your needs. For example, for the instance you provided the schema can look something like this:

    {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "characters": {"$ref": "#/definitions/characters_type"},
        "spells": {"$ref": "#/definitions/spells_type"},
        "houses": {"$ref": "#/definitions/houses_type"}
    },
    "definitions": {
        "characters_type": {
            "type": "array",
            "minItems": 0,
            "items": {
                "type": "object",
                "properties": {
                    "house": {"type": "string"},
                    "orderOfThePhoenix": {"type": "boolean"},
                    "name": {"type": "string"},
                    "bloodStatus": {"type": "string"},
                    "deathEater": {"type": "boolean"},
                    "dumbledoresArmy": {"type": "boolean"},
                    "school": {"type": "string"},
                    "role": {"type": "string"},
                    "__v": {"type": "number"},
                    "ministryOfMagic": {"type": "boolean"},
                    "_id": {"type": "string"},
                    "species": {"type": "string"}
                },
                "required": [
                    "role",
                    "bloodStatus",
                    "school",
                    "species",
                    "deathEater",
                    "dumbledoresArmy",
                    "__v",
                    "name",
                    "ministryOfMagic",
                    "_id",
                    "orderOfThePhoenix",
                    "house"
                ]
            }
        },
        "spells_type": {
            "type": "array",
            "minItems": 0,
            "items": {
                "type": "object",
                "properties": {
                    "spell": {"type": "string"},
                    "effect": {"type": "string"},
                    "_id": {"type": "string"},
                    "type": {"type": "string"}
                },
                "required": [
                    "spell",
                    "effect",
                    "_id",
                    "type"
                ]
            }
        },
        "values_type": {
            "type": "array",
            "minItems": 0,
            "items": {"type": "string"}
        },
        "houses_type": {
            "type": "array",
            "minItems": 0,
            "items": {
                "type": "object",
                "properties": {
                    "values": {"$ref": "#/definitions/values_type"},
                    "headOfHouse": {"type": "string"},
                    "mascot": {"type": "string"},
                    "name": {"type": "string"},
                    "houseGhost": {"type": "string"},
                    "founder": {"type": "string"},
                    "colors": {"$ref": "#/definitions/values_type"},
                    "school": {"type": "string"},
                    "__v": {"type": "number"},
                    "members": {"$ref": "#/definitions/values_type"},
                    "_id": {"type": "string"}
                },
                "required": [
                    "headOfHouse",
                    "houseGhost",
                    "mascot",
                    "school",
                    "founder",
                    "values",
                    "__v",
                    "members",
                    "name",
                    "_id",
                    "colors"
                ]
            }
        }
    }
    

    }

    Best Regards, Octavian