24

Say i have this short code:

item = Item.find(params[:id])
render :json => item.to_json

but i needed to insert/push extra information to the returned json object, how do i do that?

Lets say i need to insert this extra info:

message : "it works"

Thanks.

6 Answers 6

26
item = Item.find(params[:id])
item["message"] = "it works"
render :json => item.to_json
Sign up to request clarification or add additional context in comments.

6 Comments

Wouldn't this line throw and error : item["message"] = "it works" . As far as I know you can not add properties dynamically like this in ruby ( you could do something like this in js though ) .
This would work, but only if item was/is a hash. You can dynamically add properties to hashes.
Does ActiveRecord return a hash ?
I try it but I got error can't write unknown attribute. But below solving is works for me.
I think item = item.as_json need add to second line? haven't test yet
|
15

The to_json method takes an option object as parameter . So what you can do is make a method in your item class called as message and have it return the text that you want as its value .

class Item  < ActiveRecord::Base
 def message
  "it works"
 end
end

render :json => item.to_json(:methods => :message)

Comments

8

I found the accepted answer now throws deprecation warnings in Rails 3.2.13.

DEPRECATION WARNING: You're trying to create an attribute message'. Writing arbitrary attributes on a model is deprecated. Please just useattr_writer` etc.

Assuming you don't want to put the suggested attr_writer in your model, you can use the as_json method (returns a Hash) to tweak your JSON response object.

item = Item.find(params[:id])
render :json => item.as_json.merge(:message => 'it works')

4 Comments

as_json returns an array and there's no method called merge for array. there's a way to convert the item object into hash and then simply add the :message. such as render json: Hash[[*item.map.with_index]].invert[:message] = 'it works' converting into hash link
or there's another solution. just add item object into a hash, like render json: {items: item, message: 'it works'}
ActiveModel's as_json method returns a Hash.
@the_ousek It only returns an array of hashes if there is more than one result for you query. So for the particular example Steve was describing it would have indeed returned a hash because it only returns one value.
1

How to append data to json in ruby/rails 5

If you use scaffold, e.g.:

rails generate scaffold MyItem

in the view folder you will see next files:

app/view/my_item/_my_item.json.jbuilder
app/view/my_item/index.json.jbuilder

so, you can add custom data to json output for an item, just add this:

json.extract! my_item, :id, :some_filed, :created_at, :updated_at
json.url my_item_url(my_item, format: :json)    

json.my_data my_function(my_item)

As you can see, it's possible to modify as one item json output, as index json output.

Comments

0

I always use:

@item = Item.find(params[:id])
render json: { item: @item.map { |p| { id: p.id, name: p.name } }, message: "it works"  } 

Comments

-2

Have you tried this ?

item = Item.find(params[:id]) 
item <<{ :status => "Success" }

render :json => item.to_json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.