vectordotdev/vector · error

Could not convert TOML value to JSON

Error message

Could not convert TOML value to JSON

What it means

`ToValue for toml::Value` converts a TOML config value into a JSON `Value` using serde_json and panics with "Could not convert TOML value to JSON" if the conversion fails. Some TOML value shapes (e.g. datetime-local values with mixed offsets, or non-string map keys) are not directly JSON-representable, so the conversion can theoretically fail. Since `to_value` cannot return an error, the failure is a panic.

Solutions

  1. Check the TOML source for datetime or exotic-key values and convert them to plain strings before schema generation
  2. If you control the call path, pre-normalize the toml::Value via a recursive conversion that maps Datetime -> String
  3. Ensure the toml crate version in use serializes datetimes to JSON strings, or add a custom Serialize wrapper

Example fix

// before
serde_json::to_value(self).expect("Could not convert TOML value to JSON")
// after
match serde_json::to_value(self) {
    Ok(v) => v,
    Err(_) => serde_json::Value::String(self.to_string()),
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check TOML values for non-JSON-serializable shapes:
fn toml_to_json_safe(v: &toml::Value) -> Option<serde_json::Value> {
    serde_json::to_value(v).ok()
}

Type guard

fn is_json_representable(v: &toml::Value) -> bool {
    !matches!(v, toml::Value::Datetime(_))
}

Try / catch

// isolate the conversion:
let v = std::panic::catch_unwind(|| toml_value.to_value());

Prevention

When it happens

Trigger: Generating config schemas or examples from a TOML config containing values whose serde representation serde_json cannot encode — classically TOML datetimes (`1979-05-27T07:32:00`) with no matching JSON serialization, or TOML tables that serialize into non-object shapes.

Common situations: Rendering Vector config metadata from user TOML files that include TOML date-time values or exotic key types in tables; version changes in the toml crate altering datetime serialization behavior.

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/2dd5e2c468cd2cc3. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-config/src/external/toml.rs:20

use serde_json::Value;

use crate::{
    Configurable, GenerateError, ToValue,
    schema::{SchemaGenerator, SchemaObject},
};

impl Configurable for toml::Value {
    fn generate_schema(_: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
        // `toml::Value` can be anything that it is possible to represent in TOML, and equivalently, is anything it's
        // possible to represent in JSON, so... a default schema indicates that.
        Ok(SchemaObject::default())
    }
}

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

View on GitHub (pinned to bdb87aeaa4)