5
What is the right way or best practice to represent the relationships of a json resource? As an example, I have tables contrato
, representante
and empresa
and relationships contract N:1 representative and company 1:N representative.
Which of the following three ways would be recommended? Note that in the third the fields need to have a complement in the name (ex: company name) and this is a little more work, I am using this way for not using any ORM.
EDIT: In this example I am wanting to consult a contract by endpoint /contratos/1
Examples:
1:
{
"contrato_id": 1,
"numero": 123456,
"representante": {
"representante_id": 2,
"nome": "João"
"empresa": {
"empresa_id": 3,
"nome": "Empresa",
}
}
}
2:
{
"contrato_id": 1,
"numero": 123456
"representante": {
"representante_id": 2,
"nome": "João"
},
"empresa": {
"empresa_id": 3,
"nome": "Empresa",
}
}
3:
{
"contrato_id": 1,
"numero": 123456
"representante_id": 2,
"nome_representante": "João"
"empresa_id": 3,
"nome_empresa": "Empresa"
}
In order to access the url you need to publish on some public hosting, we do not have access to your localhost on port 3000.
– Marcell Alves
Ah, it was just to put the endpoint and I put the whole address. Fixed.
– Fernando Zabin
This endpoint already returns the data as JSON?
– Marcell Alves
Yes, it’s returning as in the third example.
– Fernando Zabin
It is returning "flat". This JSON structure will follow the structure defined by its backend, hierarchized according to its data modeling.
– Marcell Alves