Rails build JSON object from hash and arrays via model
<p>I am trying to post data to an API, <strong>the API is requesting a JSON request as this</strong>:</p>

<pre><code>{
    "blacklists": {
        "list1": [{
            "id": "123",
            "find": "foo",
            "type": "text"
        }],
        "list2": [{
            "id": "321",
            "find": "bar",
            "type": "text"
        }]
    }
}
</code></pre>

<p>My problem is building a JSON with hash and arrays representing the above.</p>

<p><strong>This is the code I have so far:</strong></p>

<pre><code>@blacklist = {}
@bl = {}

key_category = KeywordCategory.where("global_list = ? OR unit_id = ?",1,1)

key_category.each do |kc|

bl_name = kc.name # &lt;- "list1, list2 etc."

kc.keywords.each do |kw|
 @keywords = {}
 @keywords['id'] = kw.id
 @keywords['find'] = kw.data
 @keywords['type'] = kw.is_regexp ? "regexp" : "text"
 @bl.merge!(@keywords)
end

end

@blacklist['blacklist'] = @bl

return @blacklist.to_json
</code></pre>

<p><strong>This code gives me this JSON (which is a bit from the one the API wants):</strong></p>

<pre><code>{
"blacklists":
{"id":123,
"find":"foo",
"type":"text"
}
}
</code></pre>

<p>How do I change my code so it spits out according to the top JSON?</p>