[RUBY] I summarized the display format of the JSON response of Rails

I'm not familiar with the JSON response display format I got stuck so I organized it.

ActiveModelSerializer has three types of adapters (JSON display format).

attributes: Adapter set by default. Generate a json response without a root key. json: The response always has a root key and generates a json response json_api: A response is returned according to the display format established by the organization that determines the JSON specification called JSON API.

If you want to use: json and: json_api by default, go to the initializers directory Create a separate file such as active_model_serializers.rb (any name is OK) and Set the JSON display format there as follows.

# config/initializers/active_model_serializers.rb
ActiveModel::Serializer.config.adapter = :json_api

The actual response looks like this

[
 {"id"=>69,
  "title"=>"pot",
  "updated_at"=>"2020-10-20T20:52:09.044Z",
  "user"=>
   {"id"=>3,
    "name"=>"xu6i65h83tbvexx5dld89w39xn4u9",
    "email"=>"[email protected]"}
γ€€}
]

"articles" is the root key. The root key name is getting the controller name.

{"articles"=>
  [
    {"id"=>72,
     "title"=>"Oh yeah",
     "updated_at"=>"2020-10-20T20:58:15.458Z",
     "user"=>
      {"id"=>3,
       "name"=>"e8zu6a5m08jlgd3w1ddlxkoa",
       "email"=>"[email protected]"}
    },
  ]
}
{"data"=>
  [
    {"id"=>"66",
     "type"=>"articles",
     "attributes"=>{"title"=>"Hinkyaku", "updated-at"=>"2020-10-20T20:44:16.765Z"},
    "relationships"=>
     {"user"=>
      {"data"=>
       {"id"=>"3", 
        "type"=>"users"}
       }
      }
     }
  ]
}

It feels like it's becoming more and more structured. The fact that json_api is a display format that conforms to the JSON specifications Is it the easiest to generalize?

Recommended Posts

I summarized the display format of the JSON response of Rails
[day: 5] I summarized the basics of Java
I summarized the naming conventions for each Rails
I want to display the name of the poster of the comment
I summarized the types and basics of Java exceptions
I can't get out of the Rails dbconsole screen
[Rails] I want to display the link destination of link_to in a separate tab
I summarized the collection framework.
(Ruby on Rails6) Display of the database that got the id of the database
[Rails] How to display the list of posts by category
I want to narrow down the display of docker ps
[Rails] Check the contents of the object
Explanation of the order of rails routes
I read the source of ArrayList I read
I read the source of Integer
Check the migration status of rails
I read the source of Long
[Rails] How to omit the display of the character string of the link_to method
[Rails] I tried deleting the application
I read the source of Short
I passed the Oracle Java Bronze, so I summarized the outline of the exam.
I read the source of Byte
I summarized the flow until implementing simple_calendar in Ruby on Rails.
I read the source of String
[Ruby] Display the contents of variables
I want to display the number of orders for today using datetime.
[Rails] I will explain the implementation procedure of the follow function using form_with.
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
Display text on top of the image
The identity of params [: id] in rails
Now, I've summarized the basics of RecyclerView
part of the syntax of ruby ​​on rails
Prepare the format environment with "Rails" (VScode)
I investigated the internal processing of Retrofit
About the initial display of Spring Framework
[Rails] Change the label name of f.label
[Java] I personally summarized the basic grammar.
The process of introducing Vuetify to Rails
Format the contents of LocalDate with DateTimeFormatter
I used it without knowing the O / R mapping of rails, so I checked it.
[Rails] We have summarized the storage locations and usage of developer and user images.
[Order method] Set the order of data in Rails
I rewrote the Rails tutorial test with RSpec
I want to output the day of the week
[Rails] When I use form_with, the screen freezes! ??
I checked the place of concern of java.net.URL # getPath
[Ruby on Rails] Until the introduction of RSpec
I tried to organize the session in Rails
[Rails] Display of multi-layered data using select boxes
I compared the characteristics of Java and .NET
I want to var_dump the contents of the intent
The code I used to connect Rails 3 to PostgreSQL 10
[Ruby] Code to display the day of the week
How to display the result of form input
I touched on the new features of Java 15
I tried using the profiler of IntelliJ IDEA
I checked the number of taxis with Ruby
[Rails] Display popular posts in ranking format [Easy]
[Ruby on Rails] Individual display of error messages
Try the free version of Progate [Java I]
Rails The concept of view componentization of Rails that I want to convey to those who want to quit