Search code examples
c#json-apijsonapi-serialize

JSONAPI Complex attributes


Is this sample in a correct format based on JSON API specifications? In another word can we have in attributes an array?

{
  "meta": {
  },
  "links": {
    "self": ""
  },
  "jsonapi": {
    "version": "",
    "meta": {
    }
  },
  "data": {
    "type": "typeof(class)",
    "id": "string",
    "attributes": [
      {
        "item1": "Value1",
        "item2": "Value2",
        "item3": "Value3"
      }
    ],
    "links": {
      "self": ""
    }
  }
}

I am not sure even after reading that (link) If correct how can I Deserialize it I am using JSONAPISerializer package in C#


Solution

  • Your example is invalid.

    The value of the attributes key in a resource object must be an attributes object. It must not be an array as in your example:

    7.2.2.1 Attributes

    The value of the attributes key MUST be an object (an “attributes object”). Members of the attributes object (“attributes”) represent information about the resource object in which it’s defined.

    https://jsonapi.org/format/#document-resource-object-fields

    The value of a specific attribute may be a complex data structure:

    Attributes may contain any valid JSON value, including complex data structures involving JSON objects and arrays.

    But that would be something different than the example you have given. It would look like this instead:

    {
      "data": {
        "type": "typeof(class)",
        "id": "string",
        "attributes": {
          "foo": [
            {
              "item1": "Value1",
              "item2": "Value2",
              "item3": "Value3"
            }
          ]
        ]
      }
    }
    

    In the example given above foo would be an attribute, which has a complex data structure as a value.