Fix #1213: remove circular references when we convert to JSON