For this JSON API design problem, I have an arbitrary set of key-value pairs that need to be provided in the API Request/Response bodies. Both the keys and values are unknown. What is the best way to structure this?
As far as I can tell, there are two ways to accomplish this:
1. Undocumented object keys
{
"fruit": "Apple",
"sport": "Hockey",
...
"keyN": "valueN"
}
- PROS: Very clean, easy for application logic to parse
- CONS: This object can't be documented properly - the shape of the object is infinitely arbitrary.
2. Array of Objects
[
{
"key": "fruit",
"value": "Apple"
},
{
"key": "sport",
"value": "Hockey"
},
...
{
"key": "keyN",
"value": "valueN"
}
]
- PROS: Easy to document and understand as an array of objects with a known structure.
- CONS: Application logic will be more verbose.
What is the best way to structure this?
NOTE: This is a question about API documentation, not about application logic. As noted above, I'm well aware that #1 is the best solution for manipulation in code. But it's unclear to me just yet how to document this in API docs in such a way that will always be interpreted correctly