Search code examples
jsonjsonschema

Writing JSON schema to detect objects with duplicate names


According to the following post;

Does JSON syntax allow duplicate keys in an object?

Keys with the same name in JSON are valid, but most parsers will override the value with the last value it finds. Is there anyway in a json schema to detect duplicate names and throw an error? I want all json keys to have unique names in an object.


Solution

  • Json-schema works with valid JSON objects, so there is nothing it can do to prevent duplicate keys.

    I would suggest you to use a jsonlint as a preprocess before validating with json-schema validator. It will depend on your programming language but here you have some choices: