Represent relationships in json

Asked

Viewed 459 times

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.

  • Ah, it was just to put the endpoint and I put the whole address. Fixed.

  • This endpoint already returns the data as JSON?

  • Yes, it’s returning as in the third example.

  • 1

    It is returning "flat". This JSON structure will follow the structure defined by its backend, hierarchized according to its data modeling.

2 answers

7

The correct is the object enterprise possess a array object representative, which in turn has a array object contract:

{
    "empresa": {
        "empresa_id": 1,
        "nome": "Empresa 1",
        "representantes": [{
            "representante_id": 1,
            "nome": "João",
            "contratos": [{
                "contrato_id": 1,
                "numero": 123456
            }, {
                "contrato_id": 2,
                "numero": 654321
            }]
        }, {
            "representante_id": 2,
            "nome": "José",
            "contratos": []
        }]
    }
}
  • And when I search for a specific contract? It would not be better to represent the opposite, bringing a contract and inside it the representative and the company?

  • This research would be in charge of your backend, no? I don’t think I fully understand your scenario.

  • Yes. For example, when searching for a contract, the backend returns a json with its data and within it the data of the other related resources, in this case, representative and company. When someone seeks a contract it is essential to also bring the name of the representative and the company. So my question is what is the "correct" way to represent the relationships in this json. Using a ORM it generates a json as in the second example.

  • 1

    I understood. In this case, the navigation occurs in reverse: a contract has a representative object, which has a company object. In this way, I believe that option 2 is the most correct in your scenario.

4

Fernando, thinking of coupling, I believe that the ideal would be to have 3 interconnected information sets.

Imagine that later when you need to change information from Company / Client / Contract entities this would be less costly.

{
    "contrato":{
        "1": {
            "numero": 123456,
            "representante_id": 2,
            "empresa_id": 3
        }
    },
    "representante":{
        "2": {
            "nome": "João"
        }
    },
    "empresa":{
        "3": {
            "nome": "Empresa"
        }   
    }
}
  • I got it. Using the ORM Bookshelf.js for Node.js it generates this way, with the exception of id, which is within the entity itself. This way it seems to make more sense.

  • You could not use Bookshelf.js in your project?

  • I could, yes, I’m refactoring to use it, but the question has arisen whether you have some standard or good practice to follow. Thus, I could best represent a json resource independent of the tool used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.