Merging two JSON objects

Hey,

I got two JSON objects and would like to merge them together into a single one. Are there any helper methods that I am not seeing or do I just go ahead and do this manually?

Thanks for any pointers!

–Alex

Do you want to …

  • emit the result of the merge? => use the json builder
  • access them in a fallback fashion? => maybe the dig methods might help to have a in flight fallback of any key.
  • have a JSON::Any value? => yeah, manually I think.

a.to_h.merge(b.to_h)

You could use JSON::OnSteroids.

Basically it’s mutable JSON structure, allowing you to transform it. The documentation is still in progress but there’s a merge method built-in.

new_json = json1.on_steroids.merge!(json2)

new_json.to_json