Jbuilder: How to encode an array of hashes?

1.6k Views Asked by At

I want to encode the following JSON object using Jbuilder. How to do it?

    "should" : [
        {
            "term" : { "tag" : "wow" }
        },
        {
            "term" : { "tag" : "elasticsearch" }
        }
    ]
1

There are 1 best solutions below

0
On

Try the child! method, e.g.

output = Jbuilder.encode do |json|
    json.should do
        json.child! do
            json.term { json.tag "wow" }
        end
        json.child! do
            json.term { json.tag "elasticsearch" }
        end
    end
end

puts output

Which will output:

{"should":[{"term":{"tag":"wow"}},{"term":{"tag":"elasticsearch"}}]}