i using jbuilder ruby, , want create json hash looks this, end result:
"must" : { "ids" : { "values" : [1,2] }, "range" : { "visits" : { "gte" : 10 } } } keep in mind have no existing array iterate over. examples i've looked @ assume have array. don't. want create json on fly.
i don't recommend using jbuilder static data. whole point of jbuilder provide dsl converting complex object graphs json. in case, might convert ruby hash json directly:
require 'json' # you'll need type of json library provides `hash#to_json` { must: { ids: { values: [1, 2] }, range: { visits: { gte: 10 } } } }.to_json for learning's sake, here's how you'd build same json string jbuilder manually:
json = jbuilder.new json.set! :object json.set! :must json.set! :ids, [1, 2] end json.set! :range json.set! :visits json.set! :gte, 10 end end end.to_json # note jbuilder returns hash need converted
Comments
Post a Comment