Prompt
How do I normalize messy JSON logs from different services?
Latest observation
To normalize messy JSON logs from different services, aim to convert all events into a single canonical schema before indexing, querying, or storing them.
1) Define a canonical log shape
Pick a standard set of fields that every log should map into, for example:
{
"timestamp": "",
"service": "",
"environment": "",
"level": "",
"message": "",
"trace_id": "",
"span_id": "",
"request_id": "",
"user_id": "",
"event_type": "",
"source": "",
"raw": {}
}
Keep:
- required fields:
timestamp,service,level,message - correlation fields:
trace_id,span_id,request_id - context fields:
user_id,environment,event_type - raw: the original payload for debugging/auditing
2) Build a field-mapping layer
Different services often use different names for the same concept.
Examples:
ts,time,datetime→timestampseverity,log_level→levelmsg,message,event.message→messageservice_name,app,component→service
Create a mapping table per service or use a shared ruleset.
3) Handle nested and inconsistent structures
Logs may be deeply nested or inconsistently shaped.
Example inputs:
{ "ts": "2026-10-27T12:00:00Z", "severity": "INFO", "msg": "started" }
{ "timestamp": 1698408000, "level": "info", "message": "started", "meta": { "service": "api" } }
Normalize by:
- flattening nested fields
- extracting known keys
- preserving unknown fields in
raworattributes
4) Standardize types and formats
Convert values into consistent types:
- timestamps → ISO 8601 UTC
- log level → uppercase or lowercase consistently
- IDs → strings
- booleans/numbers → typed values, not strings
Example:
1698408000→"2026-10-27T12:00:00Z""INFO"and"info"→"info"
5) Add defaults and validation
Not every record will be complete. Apply defaults:
- missing
service→"unknown" - missing
level→"info" - missing
message→ empty string or derived from event fields
Validate output against your schema and reject or quarantine invalid records.
6) Preserve original data
Don’t throw away the original payload. Store it in:
raworiginal_event- or a dead-letter queue if parsing fails
This is essential for debugging normalization errors.
7) Use a processing pipeline
A typical pipeline looks like:
- ingest raw logs
- parse JSON
- detect service/source
- map fields to canonical schema
- transform types/normalize values
- validate
- enrich with metadata
- emit/store normalized event
8) Add enrichment if useful
You can enrich logs with:
- deployment version
- region/cluster
- host/pod/container info
- environment
- customer/tenant ID
This makes cross-service analysis much easier.
9) Example transformation
Input:
{
"ts": "2026/10/27 12:00:00",
"sev": "warn",
"text": "cache miss",
"svc": "payment-api",
"requestId": 12345,
"extra": { "user": "alice" }
}
Normalized:
{
"timestamp": "2026-10-27T12:00:00Z",
"service": "payment-api",
"level": "warn",
"message": "cache miss",
"request_id": "12345",
"user_id": "alice",
"raw": {
"ts": "2026/10/27 12:00:00",
"sev": "warn",
"text": "cache miss",
"svc": "payment-api",
"requestId": 12345,
"extra": { "user": "alice" }
}
}
10) Tools that help
Depending on your stack:
- Logstash / Fluent Bit / Vector for ingestion and transformation
- jq for ad hoc JSON reshaping
- Python / Go for custom normalization
- OpenTelemetry Logs for standardized telemetry fields
- JSON Schema / Pydantic / Zod for validation
Practical recommendation
If you’re starting from scratch:
- create a canonical schema
- implement service-specific mappers
- preserve raw data
- validate everything
- add correlation IDs consistently
If you want, I can also give you:
- a Python normalization example,
- a jq recipe, or
- a JSON Schema for normalized logs.