Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema where keys have different names

{"57ecf565817bc3932d8de349": {
  "prices": {

    "2017-07-01": {
      "summer": -1, 
      "winter": -1, 
      "xmas": -1},

    "2017-08-05": {
      "summer": -1, 
      "winter": -1, 
      "xmas": -1}
    }
  }
}

How can I write a JSON schema where each key is different (here : 2017-07-01, 2017-08-05) so I can't use items as in this example http://json-schema.org/example1.html#definitions (Set of products schema).

This is my schema :

{
  "type": "object",
  "properties": {
    "57ecf565817bc3932d8de349": {
      "type": "object",
      "properties": {
        "prices": {
          "type": "object",
          "properties": {
            "2017-07-01": {
              "type": "object",
              "properties": {
                "summer": {
                  "type": "integer"
                },
                "winter": {
                  "type": "integer"
                },
                "xmas": {
                  "type": "integer"
                }
              },
              "required": [
                "summer",
                "winter",
                "xmas"
              ]
            },
            "2017-08-05": {
              "type": "object",
              "properties": {
                "summer": {
                  "type": "integer"
                },
                "winter": {
                  "type": "integer"
                },
                "xmas": {
                  "type": "integer"
                }
              },
              "required": [
                "summer",
                "winter",
                "xmas"
              ]
            }
          },
          "required": [
            "2017-07-01",
            "2017-08-05"
          ]
        }
      },
      "required": [
        "prices"
      ]
    }
  },
  "required": [
    "57ecf565817bc3932d8de349"
  ]
}

In my original JSON I have a lot of dates like this : 2017-07-01, and I would like to avoid to repeat the schema for each date.

like image 971
mitsi Avatar asked Oct 15 '25 16:10

mitsi


2 Answers

You can do this with the additionalProperties keyword. In this example, every property in the object must validate against the given schema.

{
  "type": "object",
  "additionalProperties": {
    "type": "object",
    "properties": {
      "summer": { "type": "integer" },
      "winter": { "type": "integer" },
      "xmas": { "type": "integer" }
    },
    "required": ["summer", "winter", "xmas"]
  }
}
like image 189
Jason Desrosiers Avatar answered Oct 18 '25 11:10

Jason Desrosiers


use patternProperties

https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties

something like

{
  "type": "object",
  "patternProperties": {
    "^\d{4}-\d{2}-\d{2}": { 
      "type": "object",
      "properties": {
         "summer": {
            "type": "integer"
         },
         "winter": {
           "type": "integer"
           etc...
like image 39
Justin Maxwell Avatar answered Oct 18 '25 12:10

Justin Maxwell