Does JSON syntax allow duplicate keys in an object?

后端 未结 12 1900
青春惊慌失措
青春惊慌失措 2020-11-22 00:05

Is this valid json?

{
    \"a\" : \"x\",
    \"a\" : \"y\"
}

http://jsonlint.com/ says yes.

http://www.json.org/ doesn\'t say anyth

12条回答
  •  既然无缘
    2020-11-22 00:30

    I came across a similar question when dealing with an API that accepts both XML and JSON, but doesn't document how it would handle what you'd expect to be duplicate keys in the JSON accepted.

    The following is a valid XML representation of your sample JSON:

    
      x
      y
    
    

    When this is converted into JSON, you get the following:

    {
      "object": {
        "a": [
          "x",
          "y"
        ]
      }
    }
    

    A natural mapping from a language that handles what you might call duplicate keys to another, can serve as a potential best practice reference here.

    Hope that helps someone!

提交回复
热议问题