# Should JSON Api attributes element contain nested objects?

**URL:** <https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893>\
**Category:** Uncategorized\
**Created:** [January 28, 2017, 10:12pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893 "2017-01-28T22:12:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ifra](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@ifra](https://discuss.jsonapi.org/u/ifra)\
**Post date:** [January 28, 2017, 10:12pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/1 "2017-01-28T22:12:49Z")

</div>

This is the first time we are using JSON API in our projects and according to specification on their web, this is what a regular JSON API response should look like

```
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}

```

We are not sure, should attributes in data be always flat, or attributes could also contain nested objects such as location for example

```
"data": [{
        "type": "articles",
        "id": "1",
        "attributes": {
          "title": "JSON API paints my bikeshed!",
          "body": "The shortest article. Ever.",
          "created": "2015-05-22T14:56:29.000Z",
          "updated": "2015-05-22T14:56:28.000Z",
          "location": 
          { 
             "lat": "0.00",
             "long": "0.00"}
        },
```

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [January 29, 2017, 10:39pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/2 "2017-01-29T22:39:35Z")

</div>

I don’t think the specification explicitly forbids that, but in your case I would make `location` a relationship. I would make your API for flexible. For example, you could retrieve a list of articles written about a certain place with `/locations/id/articles`.

---

<div class="post-metadata">

**Author:** ![cosullivan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/cosullivan/32/225_2.png) [@cosullivan](https://discuss.jsonapi.org/u/cosullivan)\
**Post date:** [January 29, 2017, 11:56pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/3 "2017-01-29T23:56:38Z")

</div>

The specification actually does allow for it;  
[http://jsonapi.org/format/#document-resource-object-attributes](http://jsonapi.org/format/#document-resource-object-attributes)

> Complex data structures involving JSON objects and arrays are allowed as attribute values.

I think your approach is fine in this case.

---

<div class="post-metadata">

**Author:** ![zachaysan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/zachaysan/32/324_2.png) [@zachaysan](https://discuss.jsonapi.org/u/zachaysan)\
**Post date:** [January 30, 2017, 12:47am UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/4 "2017-01-30T00:47:19Z")

</div>

Completely agree. Either approach is acceptable.

---

<div class="post-metadata">

**Author:** ![junkoo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/junkoo/32/375_2.png) [@junkoo](https://discuss.jsonapi.org/u/junkoo)\
**Post date:** [January 31, 2017, 7:38pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/5 "2017-01-31T19:38:06Z")

</div>

When I embed complex data structures in attributes,  
do I need to use the “types” and "attributes"  
in each nested object  
like in this example:

```auto
{
"data": [
    {
      "type": "dogs",
      "id": "3f02e",
      "attributes": {
        "name": "doggy",
        "body": {
          "type": "bodies",
          "attributes": {
            "head": "small",
            "type": "heads",
            "legs": [
              {
                "type": "legs",
                "attributes": {
                  "position": "front",
                  "side": "right"
                }
              },
              {
                "type": "legs",
                "attributes": {
                  "position": "front",
                  "side": "left"
                }
              }
            ],
            "fur": {
              "type": "fur",
              "attributes": {
                "color": "brown"
              }
            }
          }
        }
      }
    }
  ]
}

```

or can I use the simpler:

```auto
{
  "data": [
    {
      "type": "dogs",
      "id": "3f02e",
      "attributes": {
        "name": "doggy",
        "body": {
          "head": "small",
          "legs": [
            {
              "position": "front",
              "side": "right"
            },
            {
              "position": "front",
              "side": "left"
            }
          ],
          "fur": {
            "color": "brown"
          }
        }
      }
    }
  ]

```

?

---

<div class="post-metadata">

**Author:** ![scottmcdonald](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/scottmcdonald/32/340_2.png) [@scottmcdonald](https://discuss.jsonapi.org/u/scottmcdonald)\
**Post date:** [January 31, 2017, 9:38pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/6 "2017-01-31T21:38:56Z")

</div>

Yes the specification allows nested complex types in attributes of an individual resource, BUT the complex type can not be another resource. See for example [Clarification on Resources Composed of Other Resources](http://discuss.jsonapi.org/t/clarification-on-resources-composed-of-other-resources/252) in the json:api forum for details on this topic. If the nested type is another resource, favor a relationship instead. A corollary to this is do not include “foreign keys” in resources, again instead favor relationships between resources that abstract foreign keys away.

---

<div class="post-metadata">

**Author:** ![michaelhibay](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/michaelhibay/32/405_2.png) [@michaelhibay](https://discuss.jsonapi.org/u/michaelhibay)\
**Post date:** [February 4, 2017, 6:09am UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/7 "2017-02-04T06:09:05Z")

</div>

From a purely design perspective (bike shedding here we GO!) I would say you would be able to do this for a string location perhaps, but I’m not sure how helpful the sub resources would be at /location/-33.234235,151.247592/…etc

---

<div class="post-metadata">

**Author:** ![spyrosn](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@spyrosn](https://discuss.jsonapi.org/u/spyrosn)\
**Post date:** [June 13, 2020, 10:55am UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/8 "2020-06-13T10:55:56Z")

</div>

I am facing the same issue @junkoo. I am thinking of keeping the same format in the nested (non-resource) object, i.e type, attributes, and relationships, simply because in my use case, the nested non-resource object can have resource objects as relationships of its own! I’m not sure the spec covers this use case, but I’m pretty sure it doesn’t expressly forbid it.

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [June 15, 2020, 10:16pm UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/9 "2020-06-15T22:16:43Z")

</div>

It looks like your API needs a design makeover.

---

<div class="post-metadata">

**Author:** ![spyrosn](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@spyrosn](https://discuss.jsonapi.org/u/spyrosn)\
**Post date:** [July 17, 2020, 7:48am UTC](https://discuss.jsonapi.org/t/should-json-api-attributes-element-contain-nested-objects/893/10 "2020-07-17T07:48:14Z")

</div>

Fwiw I ended up converting the non-resource children to resources, which greatly simplified matters.
