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

Fix #1213