how to change response format from fast_jsonapi format to AMS gem response format

1.2k Views Asked by At

I want to change The response format of fast_jsonapi gem from:

{
    "data": {
        "id": "8",
        "type": "directory",
        "attributes": {
            "firstname": "Naoufal",
            "lastname": "Huster",
        }
    }
}

To this:

{
  "id": "8",
  "firstname": "Naoufal",
  "lastname": "Huster",
}

I'm migrating from using AMS gem to using fast_jsonapi, I want to use fast_jsonapi but keep the same response json format of AMS when rendering data.

Ruby 2.5.0 Rails 5.2.1 fast_jsonapi gem

2

There are 2 best solutions below

2
On BEST ANSWER

I think this is not possible. fastjson_api gem follows the json api standard.

Also, a similar question was asked in the repo: source

0
On

You can use sparse fieldsets to selectively choose which fields you want.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
end

serializer = MovieSerializer.new(movie, { fields: { movie: [:name] } })
serializer.serializable_hash

If you need the data flattened even further, you can create a helper method to manually flatten out the JSON or you can try to flatten it using the standard .flatten function on an array or hash depending on your data format. You cannot perform flatten if you've already stringified it into JSON.