Skip to content

Instantly share code, notes, and snippets.

@anandsunderraman
Created August 24, 2022 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandsunderraman/a4570e012bb872f13bbe9c7744157b45 to your computer and use it in GitHub Desktop.
Save anandsunderraman/a4570e012bb872f13bbe9c7744157b45 to your computer and use it in GitHub Desktop.
JSON Schema for a map of map of string and string

JSON Schema for Map of Map of String to String

The above is the JSON schema that can be used to validate a map of map of string to string

Example of such a payload

{
    "key1":
    {
        "key11": "value11",
        "key12": "value12"
    }
}

Schema Explanation

Defining a schema for Map Of String to String

Schema needs to validate that it is an object. To do that we specify

"type": "object"

Schema now needs to validate the key and value are strings. additionalProperties can be used to specify that

"additionalProperties":
{
  "type": "string"
}

So we create a definition called MapStringToString

{
    "$defs":
    {
        "MapString":
        {
            "type": "object",
            "additionalProperties":
            {
                "type": "string"
            }
        }
    }
}

Defining a schema for Map of String to Map of String to String

Using the same strategy above we define an object whose additional properties specify that values can only be MapStringToString that was defined earlier

{
"$defs":
{
"MapStringToString":
{
"type": "object",
"additionalProperties":
{
"type": "string"
}
}
},
"type": "object",
"additionalProperties":
{
"$ref": "#/$defs/MapStringToString"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment