Fixing TypeError in JSON serialization