Prompt
I'm building a system with a lot of JSON logs and need schema normalization
Latest observation
Absolutely — schema normalization for JSON logs is a great idea if you want reliable search, analytics, deduplication, and downstream processing.
A practical approach is usually:
1) Define a canonical log schema
Create one normalized structure that every log event maps into, for example:
{
"timestamp": "2026-10-27T12:34:56.789Z",
"level": "info",
"service": "billing-api",
"environment": "prod",
"event_type": "payment.succeeded",
"message": "Payment processed",
"request_id": "abc123",
"trace_id": "t-456",
"user_id": "u789",
"source": {
"file": "app.py",
"line": 120
},
"attributes": {
"amount": 49.99,
"currency": "USD",
"method": "card"
}
}
2) Split fields into categories
A good normalization pattern is:
- Core fields: always present if possible
timestamp,level,message,service,event_type
- Correlation fields
request_id,trace_id,span_id,session_id
- Identity fields
user_id,tenant_id,org_id
- Source fields
host,ip,region,container_id
- Dynamic payload
attributes/datafor event-specific fields
This keeps the schema stable while still allowing flexibility.
3) Normalize inconsistent field names
Map variants into standard names:
ts,time,@timestamp→timestamplvl,severity→levelmsg,message,log→messagereq_id,requestId→request_idtraceId→trace_id
Use a field mapping layer in ingestion or ETL.
4) Standardize types
Make sure fields always have consistent types:
- timestamps → ISO-8601 UTC strings or epoch millis
- booleans → actual booleans, not
"true"/"false" - numbers → numeric, not strings
- arrays vs scalar values → choose one convention and enforce it
5) Flatten or preserve nesting intentionally
For analytics systems, flattened logs are often easier to query.
For application semantics, nested JSON may be better.
Common compromise:
- keep top-level canonical fields flat
- keep event-specific details under
attributes
6) Handle unknown/custom fields safely
Do not discard them blindly. Options:
- preserve in
attributes - maintain an
extraobject - whitelist known fields and store the rest separately
7) Version your schema
Logs evolve. Add:
schema_version- migration rules
- backward-compatible field additions whenever possible
Example:
{
"schema_version": "1.2",
"event_type": "user.login",
"attributes": {
"login_method": "sso"
}
}
8) Validate with a JSON Schema
Use JSON Schema to enforce structure and catch bad events early.
Example sketch:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["timestamp", "level", "message", "event_type"],
"properties": {
"timestamp": { "type": "string", "format": "date-time" },
"level": {
"type": "string",
"enum": ["debug", "info", "warn", "error", "fatal"]
},
"message": { "type": "string" },
"event_type": { "type": "string" },
"attributes": { "type": "object" }
},
"additionalProperties": true
}
9) Decide where normalization happens
Typical places:
- At the source: application emits normalized logs directly
- At ingestion: collector/agent normalizes
- In pipeline: ETL/stream processing normalizes after ingestion
Best practice is often:
- emit reasonably structured logs from the app
- do final normalization in the ingestion pipeline
10) Build a field dictionary
Maintain a reference document that defines:
- field name
- type
- description
- allowed values
- examples
- source mappings
This becomes the contract for producers and consumers.
If you want, I can help you with one of these next:
- design a canonical JSON log schema for your system
- create a field mapping/normalization spec
- write a JSON Schema for validation
- design a pipeline for ingesting and normalizing logs at scale
Brands