exclude certain json fields from rendering

53 views
Skip to first unread message

Zhao Difei

unread,
Nov 26, 2014, 3:03:07 AM11/26/14
to rubyonra...@googlegroups.com
Greetings all,

  I have a json object (text) to render, formatted as:

{
"a": "b",
"c":
[ { "id": "abracadabra",
"data": "good" }
{ "id": "abracxxabra",
"data": "goodsss" } ]
}

  How could exclude the id fields from rendering, I tried different combinations:

render json: obj, except: [ c: [ "id" ] ]
render json: obj, include: [ c: [ except: "id" ] ]

  none of them works, any ideas? Thanks in advance.

Cheers,
Difei

Vivek Sampara

unread,
Nov 26, 2014, 5:25:01 AM11/26/14
to rubyonra...@googlegroups.com
Your example has an error 

{ "id": "abracadabra",
"data": "good" },(comma is missing)
{ "id": "abracxxabra",
"data": "goodsss" }

Solution

render json: JSON.parse(obj).each{|k,v| v.each{|arr| arr.reject!{|x| x == "id" } } if v.class.to_s == "Array"}.to_json



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2cd584c3-a4f0-4ced-9dfa-0b89f61f89d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Nov 26, 2014, 5:57:37 AM11/26/14
to rubyonra...@googlegroups.com
If your json object is actually an ActiveRecord object (or maybe more
correctly ActiveModel) and you always want these objects to render in
a particular way, then you can override as_json to achieve this. Then
using render json to serialise it will use use overridden as_json,
where you can include whichever bits (including calculated values)
that you desire.

Colin

Zhao Difei

unread,
Nov 26, 2014, 8:00:17 AM11/26/14
to rubyonra...@googlegroups.com


On Wednesday, November 26, 2014 6:25:01 PM UTC+8, Vivek Sampara wrote:
Your example has an error 

Sorry, it's a typo. 
{ "id": "abracadabra",
"data": "good" },(comma is missing)
{ "id": "abracxxabra",
"data": "goodsss" }

Solution

render json: JSON.parse(obj).each{|k,v| v.each{|arr| arr.reject!{|x| x == "id" } } if v.class.to_s == "Array"}.to_json
Thanks but I found this a little bit ugly, so there is no way to use :except ? 

Zhao Difei

unread,
Nov 26, 2014, 8:04:31 AM11/26/14
to rubyonra...@googlegroups.com
I in fact I already override two methods of mongoid to do this, but I still have some json strings to render, which is not very handy if I cannot use the "render :except" method.

Colin

Jason Fleetwood-Boldt

unread,
Nov 26, 2014, 9:43:37 AM11/26/14
to rubyonra...@googlegroups.com
Probably a good case for an Active Model Serializer tailored to that specific use case (JSON structure)


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2cd584c3-a4f0-4ced-9dfa-0b89f61f89d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

----

Jason Fleetwood-Boldt

All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email ja...@datatravels.com with questions/concerns about this.

Reply all
Reply to author
Forward
0 new messages