0

I am working on a ruby API's.This API fetching db value and converted into a JSON format.

This is the table data enter image description here

Below code used to fetch data from db and converted into JSON format

def get_data
       
        response= data.select(:xx, :xxx, :xxx, :xxx ).where(:id => xxx1,:xx1 => xx1)
        if response.empty?
            respond_to do |format|
                msg = {:message => "No records found"}
                format.json { render :json => msg }
            end
        else
            respond_to do |format|
                format.json { render json: response.to_json }
            end
        end
    end

Now am getting the response like below

enter image description here

How can i remove the slash from the response? Thanks in advance.

Note: I was tested this API's from POSTMAN

Executed following solutions but the result was same

  1. use as_json instead of to_json

  2. removed .to_json

arj
  • 887
  • 1
  • 15
  • 37
  • try changing to_json to as_json – andylee Jan 04 '23 at 05:27
  • @andylee i was tested as_json instead fo to_json getting same response – arj Jan 04 '23 at 05:43
  • 1
    https://stackoverflow.com/questions/59926393/how-to-convert-a-hash-to-json-without-the-converted-string-being-full-of-backsla check this answer. as_json should not be adding backslashes to the result, so it seems that backslash is added before converting to json. By the way, backslash is usually ok for most of the clients as many client platform can handle escaped strings. Test on your real client instead of postman – andylee Jan 04 '23 at 05:58
  • 1
    yes you could try testing in you real client or try with something like `response = JSON.parse(data.select(:xx, :xxx, :xxx, :xxx ).where(:id => xxx1,:xx1 => xx1))` – Estebes Jan 04 '23 at 06:24
  • The value of `data1` looks like a valid string representation of a JSON hash. JSON parsing that string should return a valid hash. What do you expect the value to look like instead? Do you want to encode the string differently? Do you want to return a hash instead? – spickermann Jan 04 '23 at 06:42
  • what is the type of your db column? – Nathan Gouy Jan 04 '23 at 14:04
  • @spickermann expected out put should be [ { "id": 257, "data1": "{"tbl_name":"tbl1","tbl_name":"tbl2","tbl_name":"tbl3"}" } ] – arj Jan 05 '23 at 04:20
  • @NathanGouy it is varchar – arj Jan 05 '23 at 04:30
  • 1
    Did you consider using a [`jsonb` column type](https://guides.rubyonrails.org/active_record_postgresql.html#json-and-jsonb) in the database to store our hash structure, instead of serializing it to a `varchar`? – spickermann Jan 05 '23 at 07:27
  • that's why then If it's a string, it will be serialize as a string (even if the whole object is serialized as json, the actual value of your specific key will be a string) You could or have a jsonb column instead, or having a serializer (builder, activerecord serializer, panko ...) and do "type casting" when you serialize – Nathan Gouy Jan 06 '23 at 03:23

1 Answers1

1

I don't think you can use the variable response in a controller action.

It is already used by Rails: https://guides.rubyonrails.org/action_controller_overview.html#the-response-object

B Seven
  • 44,484
  • 66
  • 240
  • 385