ccy
ccy

Reputation: 1415

json schema for property that is not decided

I have a HTTP response body json object, of which the name of the top level property (${id}) is changing in each response. I am wondering how to describe it in json schema?

{
    "${id}": {
        "option": {
            "name": "value"
        }
     }
}

It could be:

{
    "12874349: {
        "option": {
            "name": "value"
        }
     }
}

or

{
    "12874349: {
        "option": {
            "name": "value"
        }
     },
    "12874350: {
        "option": {
            "name": "value"
        }
     }
}

Upvotes: 0

Views: 81

Answers (1)

cloudfeet
cloudfeet

Reputation: 12883

You could either use additionalProperties:

{
    "type": "object",
    "additionalProperties": {
        "type": "object",
        "properties": {
            "option": {...}
        }
    }
}

or patternProperties:

{
    "type": "object",
    "patternProperties": {
        "^[1-9][0-9]*$": {  // regex for a positive integer
            "type": "object",
            "properties": {
                "option": {...}
            }
        }
    }
}

You don't mention it in your question, but if you wanted to place limits on the number of top-level properties, you can use minProperties/maxProperties, e.g.:

{
    "type": "object",
    "minProperties": 1,  // at least one property
    "maxProperties": 5,  // no more than five at a time
    "patternProperties": {
        "^[1-9][0-9]*$": {...}
    }
}

Upvotes: 1

Related Questions