mitsi
mitsi

Reputation: 1165

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.

Upvotes: 4

Views: 3615

Answers (3)

tom redfern
tom redfern

Reputation: 31760

Even though accepted, my answer was incorrect. Instead please see Jason's answer here: JSON schema where keys have different names

Upvotes: -1

Justin Maxwell
Justin Maxwell

Reputation: 179

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...

Upvotes: 2

Jason Desrosiers
Jason Desrosiers

Reputation: 24439

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

Upvotes: 9

Related Questions