0

I have a some kind JSON structure within an array. The requirement is:

1. All the JSON object within the array can be optional.
2. Each JSON can have its own set of properties and can be complex and nested.
3. Each JSON object will have a set of mandatory attributes.

How to create a schema for such JSON. Will uisng anyOf or definitions will be helpful?

Updated: I have an array of JSON objects, where each object can have different attributes. The only attribute that will be common is 'type' with valid values as: electronics or furniture or finance. So my question is how to derive a schema?

Example

{
 "list": [
  {
   "type": "electronics"
  },
  {
   "type": "furniture"
  },
  {
   "accessRights": "readOnly",
   "rules": ['print','copy'],
   "type": "finance"
  }
}

Solution

{
"properties": {
    "list": {
        "type": "array",
        "items": {
            "type": "object",
            "required": ["type"],
            "properties": {
                "type": {
                    "type": "string",
                    "enum": ["electronics", "furniture", "finance"]
                }
            },
            "anyOf": [{
                "properties": {
                    "type": {
                        "enum": ["electronics"]
                    }
                }
            }, {
                "properties": {
                    "type": {
                        "enum": ["furniture"]
                    }
                }
            }, {
                "properties": {
                    "type": {
                        "enum": ["finance"]
                    },
                    "accessRights": {
                        "type": "string"
                    },
                    "rules": {
                        "type": "array"
                    }
                }
            }]
        }
    }
 }
}
user8479984
  • 451
  • 2
  • 9
  • 23
  • You need to be far more specific with your question. Currently it's not possible to give you any advice. – Relequestual Aug 13 '18 at 08:32
  • Sure. Let me rephrase it. Please see the updated post – user8479984 Aug 13 '18 at 21:23
  • https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required/38781027#38781027 See the "Enum" strategy. It's exactly what you need. – Jason Desrosiers Aug 14 '18 at 02:32
  • Thanks Jason. I tried and it is working fine. See the updates above. I have used anyOf and enum to arrive the solution. Is there any way, where I can state, at a time, either 'furniture' or 'electronics' block can be there. I tried using 'oneOf' within 'anyOf', but didnot work. – user8479984 Aug 17 '18 at 04:41

0 Answers0