Home > Net >  Json schema for a complex JSON?
Json schema for a complex JSON?

Time:04-20

I have a json that uses object of objects in place of an array, to easily search through the data with json keys.

How can I validate this against a schema, without hardcoding the key into the schema ? Should I be converting the object into an array in this case before validating ?

Example JSON :

{
    "item_value": {
        "id": 123,
        "name": "Item Value",
        "colors": {
            "red_value": {
                "id": 1231,
                "name": "Red Value"
            },
            "blue_value": {
                "id": 1231,
                "name": "Blue Value"
            }
        }
    },
    "another_item_value": {
        "id": 133,
        "name": "Another Item Value",
        "colors": {
            "red_value_xyz": {
                "id": 1331,
                "name": "Red Value Xyz"
            },
            "blue_value_bar": {
                "id": 1331,
                "name": "Blue Value Bar"
            }
        }
    }
}

CodePudding user response:

You can use patternProperties for RegEx-matched property validation, or if all of your properties follow the same subschema, just use additionalProperties with the subschema.

  • Related