A
A
Alexey Nikolaev2019-03-07 01:44:26
JSON
Alexey Nikolaev, 2019-03-07 01:44:26

Is it good practice to include nested entities in REST API responses?

Goodnight.
It's about something like this:

{
    "id": 1,
    "author": {
        "id": 2,
        // more data
     },
     // more nested entities
}

I read that it's not good. If so, why? I myself prefer to give only ID instead of nested objects with data.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Shumov, 2019-03-07
@inoise

I think it's bad. At worst there is HAL, at best GraphQL

D
dollar, 2019-03-07
@dollar

At one level it will look, of course, simpler:

{
    "id": 1,
    "author_id": 2,
    "author_name": "Vasya",
     // more data
}

But in general it depends on the quantity and quality of the data. Everyone decides for themselves, but personally I think that somewhere no more than 20 key-value pairs per level is convenient. That is, if you have, say, 100 pairs, then you can already scatter them into categories.
Or, say, if one of the fields is an array - then God himself ordered. For example, like this:
{
    "id": 1,
    "authors": [
        { "id": 2, "name": "Vasya" },
        { "id": 3, "name": "Petya" },
     ],
     // more nested entities
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question