When designing a JSON interface, are there any reasons to choose
{
"top-level-wrapper-key": {
"a_key": "a_value",
"another_key": "another_value"
}
}
over
{
"a_key": "a_value",
"another_key": "another_value"
}
(short of subjective decisions based on the nature of the data itself)?
According to my reading of the JSON spec, both are allowed:
"An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace. Each name is followed by :colon and the name/value pairs are separated by ,comma."
I seem to remember some tools only accepting JSON that has the top-level-wrapper key, but I haven't seen this in a while.
I have tried several json validation tools (e.g python -m json.tool
and online tools like JSONLint), which all seem to accept both versions.
Researching this I stumbled upon an old vulnerability when the top-level JSON is an array rather than an object, which seems to be patched now in all major browsers.
Was there a change in the JSON spec that lead to this or have I just run into more opinionated JSON implementations in the past?