can i have json without root element?
mahavir155 opened this issue · 8 comments
Can i have json without root element?
Eg: I am getting following:
{"user":{"id":6,"mobile":"+919420101252","auth_token":"5VGal6JXu6qvZB8KIiULWQtt"}}
I would like to have only
{"id":6,"mobile":"+919420101252","auth_token":"5VGal6JXu6qvZB8KIiULWQtt"}
I tried setting following in initializers:
ActsAsApi::Config.include_root_in_json_collections = false
But seems to be not working.
Any other way or it is not possible?
Though, thank you for nice library :).
can anyone answer this please?
Could you please provide some more information like your Rails version or the used code in your controller?
Rails 4.1.6
Controller code:
class Api::AuthenticationController < ActionController::Base
respond_to :json
def get_seller_associations
mobile = params[:phone]
contacts = params[:contacts]
contacts_array = contacts.split(",")
associations = Array.new
$i=0
while $i < contacts_array.length do
c = Api::ApiHelper.format_mobile(contacts_array[$i])
if Buyer.exists?(:mobile => c) then
associations << c
end
$i+=1
end
mobile = Api::ApiHelper.format_mobile(mobile)
if Seller.exists?(:mobile => mobile) then
@seller = Seller.where(:mobile => mobile).take
@seller.auth_token = Seller.generate_auth_token
@seller.associations = associations
@seller.save
else
@seller = Seller.new
@seller.auth_token = Seller.generate_auth_token
@seller.mobile = mobile
@seller.associations = associations
@seller.save
end
respond_with(@seller, :location => nil) do |format|
format.json { render_for_api :seller_detail, :json => @seller, :root => :user }
end
end
end
Thank you for reply. Please let me know if you need more information..
Well, your code sets the root
property, when rendering the response, iirc this will overwrite the default settings.
format.json { render_for_api :seller_detail, :json => @seller, :root => :user }
Try the remove the :root => :user
setting.
Thanks again for reply!
If i remove :root => :user, it seems to be adding model-name like below:
{"seller":{"id":4,"mobile":"+919420730595","auth_token":"ZZmANCqWgKfiOR1zLrk1Bgtt","associations":["+919423493227"]}}
What i want is simply:
{"id":4,"mobile":"+919420730595","auth_token":"ZZmANCqWgKfiOR1zLrk1Bgtt","associations":["+919423493227"]}
Is it possible?
Ok, I solved it by adding following in /config/initializers/acts_as_api.rb
ActsAsApi::Config.add_root_node_for = []
ActiveRecord::Base.include_root_in_json = false
As per metioned in #30
Thanks again for looking into it!
Awesome! I'll close this ticket then.