4
{"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.

mitsi
  • 1,005
  • 2
  • 11
  • 15
  • Your property names are completely dynamic. There is no way you can write a schema for this. – tom redfern Sep 29 '16 at 14:39
  • @TomRedfern there is no way to write a schema even if its the same structure for each date ? – mitsi Sep 29 '16 at 14:50
  • No. See my answer below. You should be defining a STATIC data structure. – tom redfern Sep 29 '16 at 15:00
  • For a similar question with a similar answer: [How would you design JSON Schema for an arbitrary key?](https://stackoverflow.com/questions/16222633/how-would-you-design-json-schema-for-an-arbitrary-key) – Jonatan Lindén May 11 '22 at 10:43

3 Answers3

9

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"]
  }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
1

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...
Justin Maxwell
  • 159
  • 2
  • 9
-1

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

tom redfern
  • 30,562
  • 14
  • 91
  • 126