6
We recently requested a service for a partner company, to consult with some people. The service was designed from scratch to meet a particular system demand of ours. This is a simple Rest service, using JSON as return.
However, the result of the service provided caught my attention. See an example:
{
"73317882484": {
"nome": "Joaquim"
},
"55968857463": {
"nome": "Maria"
}
}
Although it is a valid Json, the developer understood that the CPF field would be a kind of key to group the other person’s information. So instead of him creating a field "cpf"
with the value of the CPF, it adopted the CPF itself as the field name. Also, instead of creating an array for each record, a single object was returned.
Everything indicates that the returned Json has been manipulated, without any help from a framework to be generated, because it would be weird to map this Json to a Java class (for example) without knowing all the possible CPF returns. Anyway, it doesn’t make much sense.
I expected, as I mentioned before, something like this:
[
{
"cpf: "73317882484"
"nome": "Joaquim"
},
{
"cpf: "55968857463"
"nome": "Maria"
}
]
The question is: how to explain that the format mentioned before is not suitable for a return?
I did not find any kind of reference on this subject. In a brief dynamic reading by RFC I also found nothing associated with this.
Where exactly JSON is no longer appropriate?
– Woss
In fact you can map in Java yes... What you can do is provide a documentation of the expected return and give examples
– Sorack
@Sorack, I’ve never seen this format before. It’s possible to read with Jackson? I hope you’re not talking about I also need to manipulate the Json string... rs.
– Dherik
@Andersoncarloswoss, I believe I answered your question in my question. Which point was unclear? One of the arguments is that I don’t see how it would be possible to read this in a json "parser" framework, having at the end a list of people with CPF and other information.
– Dherik
@Dherik yes, it is possible. In this case you would map this attribute to a
Map<String, Pessoa>
or something similar to this– Sorack
Being a valid JSON, any Java-worthy tool to work with JSON will be able to analyze the data correctly. What I see is that maybe this isn’t the easiest way for you, so I wondered, to see if it was clear to you what the limitations were. Being clear this, your answer would be ready.
– Woss
@Sorack, I ran a test here, it actually does. It’s just that Anderson said, it’s possible, but in my case it’s not a convenient format. Again, I had never seen this way of using Json before and immediately assumed it as "incorrect" (which is the main point of the discussion)
– Dherik
Perhaps a valid argument is semantics: to request a list of people is different from requesting a map of people. If I am interested in the whole, it makes sense that I have a list, if I am interested in insolated information, it makes more sense that I have a map.
– Woss
Apparently it lacked scope on the integration, publish exactly the snippet that refers to the JSON that should be returned, suddenly it was requested a "valid JSON" and it was only in that that he cared. If that was the case, add the term list to the request and show an example.
– user38174