vectordotdev/vector · error

Could not convert time zone to JSON

Error message

Could not convert time zone to JSON

What it means

In vector-config's schema generation, `TimeZone`'s `ToValue` impl converts the time zone into a JSON `Value` via serde_json and panics with "Could not convert time zone to JSON" if serialization fails. Time zones are expected to always be JSON-serializable (as an IANA string), so failure signals a genuinely unexpected state. Because `to_value` returns `Value` (not Result), the failure is a panic rather than an error return.

Solutions

  1. Inspect the TimeZone value being serialized (custom serde impls or wrappers can break JSON representability)
  2. Ensure no custom Serialize impl for TimeZone writes non-JSON-compatible data (e.g. maps with non-string keys)
  3. Replace the expect with error propagation if your wrapper allows returning a Result

Example fix

// before
serde_json::to_value(self).expect("Could not convert time zone to JSON")
// after
serde_json::to_value(self).unwrap_or(serde_json::Value::String(self.to_string()))
Defensive patterns

Strategy: type-guard

Validate before calling

// Confirm serializability before to_value:
serde_json::to_value(&tz).map_err(|e| eprintln!("timezone not JSON-serializable: {e}")).ok();

Type guard

fn is_json_safe<T: serde::Serialize>(v: &T) -> bool { serde_json::to_value(v).is_ok() }

Try / catch

// to_value panics; isolate calls:
let result = std::panic::catch_unwind(|| tz.to_value());

Prevention

When it happens

Trigger: Calling `TimeZone::to_value()` during config schema generation or metadata extraction when serde_json cannot serialize the TimeZone's serde representation.

Common situations: Building schema/config metadata for components whose config includes chrono time zones; effectively never hit in practice — usually only when a custom serialization override or unusual TimeZone payload breaks the invariant.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16). Data as JSON: /api/errors/fe4feabce088b7dc. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-config/src/external/datetime.rs:69

        apply_metadata(&mut local_schema, local_metadata);

        let mut tz_metadata = Metadata::with_title("A named timezone.");
        tz_metadata.set_description(
            r#"Must be a valid name in the [TZ database][tzdb].

[tzdb]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"#,
        );
        tz_metadata.add_custom_attribute(CustomAttribute::kv("logical_name", "Named"));
        let tz_schema =
            get_or_generate_schema(&Tz::as_configurable_ref(), generator, Some(tz_metadata))?;

        Ok(generate_one_of_schema(&[local_schema, tz_schema]))
    }
}

impl ToValue for TimeZone {
    fn to_value(&self) -> Value {
        serde_json::to_value(self).expect("Could not convert time zone to JSON")
    }
}

View on GitHub (pinned to bdb87aeaa4)