0

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#

tulipe
  • 676
  • 2
  • 8
  • 22

2 Answers2

1

Why aren't you sure? Here's a quote from JSON API specification:

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

Class System.Text.Json.JsonSerializer can deserialize a JSON array into a C# IEnumerable<T>. So you may create an object that has a property Attributes of type IEnumerable<T> and deserialize like this:

using System.IO;
using System.Text.Json;

// ...

string json = File.ReadAllText("YourJsonDocumentPath");

YourEntityDescribedInJsonDocument obj = JsonSerializer.Deserialize<YourEntityDescribedInJsonDocument>(json, new JsonSerializerOptions());
SNBS
  • 671
  • 2
  • 22
  • What makes me confused is that I am trying to serialize List so what I am doing is that I create DocumentRoot> then when I use JsonConvert.SerializeObject this crash because I miss the ID – tulipe Dec 03 '22 at 12:22
  • I want finally to Serialize my list of classes to the format above and I am not able to. Any advise? – tulipe Dec 03 '22 at 12:25
  • What ID are you missing? – SNBS Dec 03 '22 at 12:52
  • I have the following list of this class public class Root { public string item1 { get; set; } public string item2 { get; set; } public string item3 { get; set; } } I want to convert it to the format above so I created new DocumentRoot> and fill with data. When I try to serialize it it crash JsonConvert.SerializeObject(root, this.settings); with exception of "Resource identifier objects MUST contain 'id' members." – tulipe Dec 03 '22 at 13:18
  • @tulipe If it needs ID member, create it. It's a good way of debugging. – SNBS Dec 03 '22 at 14:43
  • To add an ID you need to encapsulate everything in a class which when you serialze add a layer inside attributes. Can you propose a solution whithout adding extra fields – tulipe Dec 03 '22 at 16:04
  • Again — refer to the [specification](https://jsonapi.org/format/#document-resource-objects). It is said there that resource objects *must* contain ID. So I'm afraid you have to add it. – SNBS Dec 03 '22 at 16:50
  • 1
    @tulipe Your issue seems to be rooted in trying to serialize resource objects as a list of attributes of another resource object. That is going against the intent of the specification. And the structure you are trying to use for it violates the JSON:API specification also on a semantical perspective. Please have a look on my new answer. – jelhan Jan 28 '23 at 21:01
1

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.

jelhan
  • 6,149
  • 1
  • 19
  • 35