What is the correct way or best practice for representing the relationships of a resource in json? As an example, I have the tables contrato
, representante
and empresa
and the relationships contract N: 1 representative and company 1: N representative.
Which of the 3 following ways would be recommended? Note that in the third the fields need to have a complement in the name (eg: company_name) and this is a bit more laborious, I am using this way because I do not use any ORM.
EDIT: In this example I'm looking to query a contract with the 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"
}