code: def to_json(payload, mode="history"):
      
      for key, val in six.iteritems(payload):
          if isinstance(val, dict):
              payload[key] = to_json(val, mode)
          else:
              payload[key] = val_to_json(
                  key, val, mode, step=payload.get("_step"))
      return payload
docs: Converts all keys in a potentially nested array into their JSON representation