I will get around the problem by subclassing OpenStruct as follows:
class DataStruct < OpenStruct def as_json(*args) super.as_json['table'] end end
then you can easily convert to JSON like this:
o = DataStruct.new(a:1, b:DataStruct.new(c:3)) o.to_json # => "{\"a\":1,\"b\":{\"c\":3}}"
Carefully? Therefore, in answering your question, you should write this instead:
[{:a => :b}].collect {|x| DataStruct.new(x)}.to_json
gives you:
=> "[{\"a\":\"b\"}]"
Kevin hutchinson
source share