Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can I detect duplicate keys in JSON request body?

I know the RFC states "The names within an object SHOULD be unique" but most parsers, including the one used by Edge, just use the last occurrence of the duplicate.

I would like to detect this situation and raise a fault 400 Bad Request.

I'm validating my request using my Open API Spec and tv4, but it does not detect the duplicates either.

Here's what I'm thinking:

  1. Use a JSON parser that detects duplicates
  2. Use a JSON validator that detects duplicates
  3. Compare original request with JSON.stringify(JSON.parse(request.content), but original request may be a "pretty" version so simple string comparison won't work.

Anyone else solve this problem?

2 8 34.8K
8 REPLIES 8