Q/A - Add key-value before attribute
lironesamoun opened this issue · 2 comments
Hi,
I would like to add for example a key-value at the same level of type and Id like here:
{
"data": {
"type": "house",
"id": "5",
"date_time" : "date_time",
"attributes": {
"name": "name"
},
"relationships": {
"site": {
"links": {
"self": "http://127.0.0.1:8000/api/v1/house/5/relationships/location",
"related": "http://127.0.0.1:8000/api/v1/house/5/location"
}
}
},
"links": {
"self": "http://127.0.0.1:8000/api/v1/house/5"
},
"meta": []
}
}
Is it possible to do so ?
Why ? Because the date time is not part of my house object and it is something important for me to add.
I was thinking to put it in meta but maybe it's better at the same level of ID and type.
Any insight ? What do you think ?
Thank you very much
Hi! The meta
object exists for exactly this kind of scenario - it contains "non-standard meta-information about a resource that can not be represented as an attribute or relationship."
For resource objects we only support the top-level keys defined in the specification, so you should use the meta
key in this scenario.
In addition: Putting custom attributes at the same level as type
and id
is forbidden in the JSONAPI standard: https://jsonapi.org/. You should put them in attributes
if it is part of your model. If it isn't, it should be in meta
.