How can I stop Grape from JSON-encoding my output twice?

292 views Asked by At

I'm currently implementing a JSON based Grape API. I've integrated roar for JSON serailization and deserialization. When executing the following code, it seems that the JSON is encoded twice resulting a response with slashes. Is there anyway on Grape to suppress the JSON decode when required?

json = UserRepresenter.new(user).to_json { status: StatusResponse::VALID, message: json }

1

There are 1 answers

0
AudioBubble On

Grape automatically encodes the output as JSON (when format :json is specified) so the to_json call in your code is superfluous.

Try changing your code to simply this:

{ status: StatusResponse::VALID, message: UserRepresenter.new(user) }