Output json for entity with relationship in value object


#1

Hi!
I have two resources: Seller and Post.
Seller does not contain whole Post - it includes array of value objects, called appointment.
This way, Seller look like this:

{
  id: 1,
  name: 'seller1',
  appointments: [
    { post: { id: 1, name: 'post1' }, date: '2018-09-09T03:46:26.915Z' },
    { post: { id: 2, name: 'post2' }, date: '2018-10-09T03:46:26.915Z' },
  ],
}

I can’t figure it out how output json must be looks like, if appointment is not resource?


#2

The whole post doesn’t have to be included

validated with https://jsonapi-validator.herokuapp.com/:

   { 
    "data": {
    "attributes": {
      "name": "Seller 1"
    },
    "id": "1",
    "relationships": {
      "appointments": {
        "data": [
          {
            "id": "1",
            "type": "Post"
          }
        ],
        "links": {
          "self": "/Seller/1/appointments"
        }
      }
    },
    "type": "Seller"
  },
  "included": [
    {
      "attributes": {
        "date" : "0000"
      },
      "id": "1",
      "type": "Post",
      "links": { "self" : "/Post/1" }
    }
  ]
}

#3

Thanks for response, Thomaxxl!

I don’t understand your example:

{
  "data": {
    "attributes": {
      "name": "Seller 1"
    },
    "id": "1",
    // code before means seller looks like:
    // { id: 1, name: 'Seller 1' }
    "relationships": {
      "appointments": {
        "data": [
          {
            "id": "1",
            "type": "Post"
          }
        ],
        "links": {
          "self": "/Seller/1/appointments"
        }
      }
    },
    "type": "Seller"
    // code before means seller looks like:
    // {
    //   id: 1,
    //   name: 'Seller 1',
    //   appointments: [
    //     { id: 1 }
    //   ]
    // }
  },
  "included": [
    {
      "attributes": {
        "date": "0000"
      },
      "id": "1",
      "type": "Post",
      "links": { "self": "/Post/1" }
    }
  ]
    // code before means seller looks like:
    // {
    //   id: 1,
    //   name: 'Seller 1',
    //   appointments: [
    //     { id: 1, date: '0000' }
    //   ]
    // }
  }

Deserialized seller has appointments array, that contains only post and post contains only date. But existing seller has appointments array, that contains post and date, post contains only name.

// deserialized example
{
  name: 'Seller 1',
  id: '1',
  appointments: [
    { date: '0000', id: '1' }
  ]
}
// original seller
{
  name: 'Seller 1',
  id: '1',
  appointments: [
    { post: { id: 1, name: 'Post 1' }, date: '0000' }
  ]
}

If I decide to appointment being the resource, json will looks like this:

{
  "data": {
    "type": "sellers",
    "id": "1",
    "links": {
      "self": "http://localhost/sellers/1"
    },
    "attributes": {
      "name": "seller1"
    },
    "relationships": {
      "appointments": {
        "data": [
          {
            "type": "appointments",
            "id": "1"
          },
          {
            "type": "appointments",
            "id": "2"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "appointments",
      "id": "1",
      "links": {
        "self": "http://localhost/sellers/appointments/1"
      },
      "attributes": {
        "date": "2018-09-09T03:46:26.915Z"
      },
      "relationships": {
        "post": {
          "data": {
            "type": "posts",
            "id": "1"
          }
        }
      }
    },
    {
      "type": "appointments",
      "id": "2",
      "attributes": {
        "date": "2018-09-10T03:46:26.915Z"
      },
      "links": {
        "self": "http://localhost/sellers/appointments/1"
      },
      "relationships": {
        "post": {
          "data": {
            "type": "posts",
            "id": "2"
          }
        }
      }
    },
    {
      "type": "posts",
      "id": "1",
      "attributes": {
        "name": "post1"
      },
      "links": {
        "self": "http://localhost/post/1"
      }
    },
    {
      "type": "posts",
      "id": "2",
      "attributes": {
        "name": "post2"
      },
      "links": {
        "self": "http://localhost/post/2"
      }
    }
  ]
}

If appointment is not resource means, that it is not contained in “relationships” and “included” props of json. So this is the place where I stuck.