title: Serializing JSON to JSON API format in Ruby/Rails
body: <p>In my Rails 5 API project I am consuming SOAP services. With the use of the Savon gem, version 2, I parse the response from any SOAP web service to JSON format. </p>
  
  <p>In my Rails project I am creating a facade for multiple SOAP services. Let's say 4 SOAP services that have 90% similar response format/structure. I would like my facade to be accepting and sending out the information from the services in JSON API format. </p>
  
  <p>I found some solutions, but not an ideal and effective one:</p>
  
  <ol>
  <li>Parsing the JSON/SOAP response from the SOAP service into models/objects, and then outputting this to JSON API through existing libraries (e.g. jsonapi-resources gem)</li>
  <li>Building a serializer from scratch within Rails</li>
  <li>Use an ESB</li>
  </ol>
  
  <p>The first option seems to be overkill and error prone.</p>
  
  <p>The second option seems to be a very time consuming project, and it'll also be prone to a lot of errors if I have to do this by myself in a short amount of time.</p>
  
  <p>The third option is a no go due to budget.</p>
  
  <p>So my question is, do there exist tools out there that can assist me? I have been doing a great deal of research, but have not been able to find anything that seems to be a solution to my problem or fulfilling my needs. </p>
  
  <h2>Solution so far</h2>
  
  <p>I found it most pragmatic to go with option 1. I am doing something similar to the following:</p>
  
  <pre><code># app/controllers/api/v1/some_resources_controller.rb
  ...
  # Retrieve response from service
  response = fetch_resources_from_service(query)
  # Pick out the values from the response that we want to output
  resource_params = strip_response(reponse, whitelist: [:id, :title, :date, ...]
  # Create a model object from these stripped/whitelisted params
  resource = Api::V1::SomeResource.new(resource_params)
  # Render in JSON API format (with active_model_serializer)
  render json: resource, adapter: :jsonapi, serializer: Api::V1::SomeResourceSerializer
  </code></pre>