0

We have a caching layer that stores json output of strings. I'd like to be able to put these strings into an array which I then transform to json via .to_json but it escapes all the previously encoded json. Is there a way to avoid this?

Here's a sample action to explain:

def index
  a={name:"jon", email:"jon@domain.com"}.to_json
  r={}
  r[:users]=[]
  r[:users] << a
  render json: r.to_json
end

Outputs:

{"users":["{\"name\":\"jon\",\"email\":\"jon@domain.com\"}"]}

But I want:

{"users":["{"name":"jon","email":"jon@domain.com"}"]}

Though I am not showing it here, I'd be open to using ActiveModelSerializer (the 0.8 branch)

Edit

One possibility is doing a JSON.parse but obviously, that's a bit of a performance hit which I'd like to avoid.

halfer
  • 19,824
  • 17
  • 99
  • 186
timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

0

You are converting a json object to another json object.. Try replacing

   a={name:"jon", email:"jon@domain.com"}.to_json

by

    a={name:"jon", email:"jon@domain.com"}
  • right, so I'm showing that because we are caching JSON in redis so it is a JSON string. Was just showing it for clarity's sake. Don't want to but could JSON.parse it but that introduces it's own issues. It does bypass the 20 sql calls that are done in generating these objects – timpone Nov 04 '15 at 20:59