The following json text is the result of a call to eBay’s search API.
Q: Is this json badly constructed? (It’s technically, correct – so that’s not what I’m referring to)
By that I mean, notice how -every value- is inside an array? instead of just being a "key" : "value"
?
eg.
"ack": [
"Success"
],
or
"version": [
"1.12.0"
],
etc..
Now, before you answer "well, maybe each key has multiple result values" .. I’m pretty sure most of them can’t.
(this json schema is really making my life a pita. yes, it’s easy to make each POCO property a List<string>
but it’s the point of it all!)
References: Here’s the office eBay API documention for this endpoint
2
Answers
I normally use this tool for see if a JSON is good constructed:
http://json.parser.online.fr/
In your case, is not correct. You should store the data by Keys and Values.
After reading the documentation, I understand eBay’s approach, but I find it poor in terms of client side deserialisation. For example, the benefit of an array value for the
ack
property is that the value could also contain a warning, e.g.:However, a list of strings is not ideal for client side processing (e.g. in C#,
bool hasWarning = pocoList.Contains("Warning");
doesn’t strike me as completely foolproof). I’d rather have a response such as:Then with my deserialised POCO, given that the value of
warning
is still a string, I could write this:This would allow me to replace my previous LINQ query with
bool hasWarning = ack.HasWarning;
.There are definitely places where use of arrays is completely unnecessary. The docs describe the
version
property as “the release version that eBay used to process the request”, thus I would return this as a single string. The array would only make sense if it was aversions
property (e.g. for identifying all versions of the backend that support a specific request).I’ve definitely seen worse JSON responses, but there are places where the APIs should ideally be returning a JSON object or a single value.