You can do it like this:
require "json"
json = JSON.parse(%({"foo": 1}))
json.as_h["bar"] = JSON::Any.new(2_i64)
puts json.to_json
It’s a bit cumbersome but there’s no other way because Crystal is statically typed.
We could consider adding more and more methods to JSON::Any
, like []=
but I don’t know.