Search code examples
jsonjsonschemajson-schema-validatorjson-schema-defaults

Create JSON schema based on a json object


Given the following JSON object, how can I build json schema? Product1, Product2 and Product3 are dynamic "keys" and I could have many more like that, but each of them will have the same "value" object with required keys as packageId1, packageId2, packageId3 and their corresponding values as strings.

{
  "Product1": {
    "packageId1": "basicpackage",
    "packageId2": "basicpackage",
    "packageId3": "basicpackage"
  },
  "Product2": {
    "packageId1": "newpackage",
    "packageId2": "newpackage",
    "packageId3": "newpackage"
  },
  "Product3": {
    "packageId1": "thirdpackage",
    "packageId2": "thirdpackage",
    "packageId3": "thirdpackage"
  }
}

Solution

  • I think I figured how to do it. In case anyone is interested, I am answering my own question. Also, I welcome better suggestions.

    {
      "title": "JSON Schema for Fulfillment Config",
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "patternProperties": {
        ".{1,}": {
          "type": "object",
          "properties": {
            "packageId1": { "type": "string" },
            "packageId2": { "type": "string" },
            "packageId3": { "type": "string" }
          }
        }
      }
    }