# How to get relationship data without including them?

**URL:** <https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110>\
**Category:** Uncategorized\
**Created:** [December 29, 2020, 10:59am UTC](https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110 "2020-12-29T10:59:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JMQuentic](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jmquentic/32/720_2.png) [@JMQuentic](https://discuss.jsonapi.org/u/JMQuentic)\
**Post date:** [December 29, 2020, 10:59am UTC](https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110/1 "2020-12-29T10:59:39Z")

</div>

Hey there, we want to use JSON:API but at this point we struggle with the specification.

I want to make the following request:

> GET /articles

The response should include the relationships data (id and type). I couldn’t figure out if this is either forbidden, allowed or optional.

Is there any way to **force** the inclusion of the data in the relationships without actually include them?  
I do not want to make the following request:

> GET /articles?include=author

This would create a included object with every field on the author. I could use sparse fieldsets:

> GET /articles?include=author&fields[author]=id

This would prevent every field to be included but all I want is the linkage data (id, type) for the relations and no further info.

Is there any special way to achieve this?

---

<div class="post-metadata">

**Author:** ![jelhan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jelhan/32/822_2.png) [@jelhan](https://discuss.jsonapi.org/u/jelhan)\
**Post date:** [January 10, 2021, 10:40am UTC](https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110/2 "2021-01-10T10:40:29Z")

</div>

The JSON:API specification does not allow a client to request usage of resource linkage for a relationship without requesting the related resources to be included.

It’s up to the server implementation to decide if it provides [related resource links](https://jsonapi.org/format/#document-resource-object-related-resource-links) or [resource linkage data](https://jsonapi.org/format/#document-resource-object-linkage) or both for a specific relationship. Only if a related resource is included resource linkage must be used.

A server may even decide to not include the relationship field by default. As pointed out in the question you can force the server to include that field using [Sparse Fieldsets](https://jsonapi.org/format/#fetching-sparse-fieldsets).

In general it’s considered good practice to

- provide related resource links for all relationships,
- include resource linkage data only if
  - the related resource is included or
  - it does not cause additional load for the server.

---

<div class="post-metadata">

**Author:** ![lode](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/lode/32/592_2.png) [@lode](https://discuss.jsonapi.org/u/lode)\
**Post date:** [March 12, 2021, 11:57am UTC](https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110/3 "2021-03-12T11:57:31Z")

</div>

> [@JMQuentic](#):
>
> GET /articles?include=author&fields[author]=id

This would work as I understand the spec. You can (maybe should) even remove the `id` part from it. The `type` and `id` are not part of the fields ([JSON:API — Specification v1.1 (Archived Copy)](https://jsonapi.org/format/1.1/#document-resource-object-fields)) of a resource.

However, good to know that the sparse fieldset work with the `type` of the (related) data, not the relationship name of it. Thus if the `type` of the `author` relationship is `human` you need to use `human` for the sparse fieldset.

Thus `/articles?include=author&fields[human]=` would include the relationship but not the related data. Note the author does need to be present in the `included` array, but without any extra data.

```auto
{
    "data": [
        {
            "type": "article",
            "id": "1",
            "attributes": {
                "title": "foo"
            },
            "relationships": {
                "author": {
                    "data": {
                        "type": "human",
                        "id": "42"
                    }
                }
            }
        },
        {
            "type": "article",
            "id": "2",
            "attributes": {
                "title": "bar"
            },
            "relationships": {
                "author": {
                    "data": {
                        "type": "human",
                        "id": "42"
                    }
                }
            }
        }
    ],
    "included": [
        {
            "type": "human",
            "id": "42",
            "attributes": {}
        }
    ],
    "links": {
        "self": "/articles?include=author&fields[human]="
    }
}

```

Another option is indeed that the server decides to not include full resources but only resource identifiers. Then you can also avoid having the `included` array and the sparse fieldset wouldn’t be needed anymore.

---

<div class="post-metadata">

**Author:** ![Bernhard](https://avatars.discourse-cdn.com/v4/letter/b/c37758/32.png) [@Bernhard](https://discuss.jsonapi.org/u/Bernhard)\
**Post date:** [January 11, 2022, 10:26am UTC](https://discuss.jsonapi.org/t/how-to-get-relationship-data-without-including-them/2110/4 "2022-01-11T10:26:01Z")

</div>

If a client just wants to include the relationship id objects of the article resource without including the related resource(s), it can issue a request as

> [@](#):
>
> GET /articles?fields[article]=author
