Netflix / fast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.
Apache License 2.0
5.07k stars 425 forks source link

How to construct custom JSON #204

Closed JonathanJumper closed 6 years ago

JonathanJumper commented 6 years ago

Hey, I'm new to serializers, I have some models and some relations within,

I'm trying to construct the json response but I don't know how to make it in the way I want (front end expects)

Here is what I've got so far in Offer json:

{
  "data": {
    "id": "1",
    "type": "offer",
    "attributes": {
      "id": 1,
      "amount": 12,
      "date": "2018-04-10 00:00:00 UTC",
      "accompanied": "Jugo de lulo, Arroz caribeño",
      "price": 12000
    },
    "relationships": {
      "dish": {
        "data": {
          "id": "1",
          "type": "dish"
        }
      }
    }
  }
}

And this is what I want

{
        id: '1',
        dish_id: '2',
        dish_name: 'Pasta Carbonara',
        dish_photo: ''string'
        available: 24,
        date: '12/12/2018',
        price: 12000,
        experience: ['delivery', 'pickup'],
        chef_id: '1',
        chef_name: 'Juan Cocinero',
        chef_photo:  'string',
}
zinosama commented 6 years ago

This gem follows the json api (http://jsonapi.org/) standard, whereas your desired output format doesn't. I'd recommend writing your own serializer or using AMS (https://github.com/rails-api/active_model_serializers/tree/v0.10.6/docs).