This doesn't actually perform the `compact` on result, but only returns a compacted copy of the result array. I think you want `compact!`.

Rather than use `compact!`, I would combine it with the following line to yield `result.compact.to_json` which I feel reads a bit better.