IMD SolutionComputing LibraryShop main store

Troubleshooting

Why Telegraf misses a nested JSON key set as an MQTT tag

Edge computing and IIoT Published 6 min read

Short answer

Set tag_keys to the flattened nested key: Telegraf's JSON parser joins object names with underscores, so message.basic_container.station_type reaches tag matching as message_basic_container_station_type. With that selector and json_time_format set to unix_ms for the 13-digit millisecond stamp, the station type is written as a tag and the remaining payload values stay fields. Prove it with a file-input test that prints line protocol before reconnecting the database output.

On this page
  1. The parser flattens nested names before tag matching
  2. Set the selector to the flattened name
  3. Prove the parse without the broker
  4. The broker path is a separate checkpoint
  5. Scope to a subtree only when the subtree is the point
  6. Decide by symptom
  7. Questions asked

Telegraf never applies tag_keys to the names written in your payload; it applies them to the names its JSON parser has already produced. The parser flattens nested objects into single keys joined with underscores, so message.basic_container.station_type reaches tag selection as message_basic_container_station_type, and a selector written for the dotted path matches nothing. The value then stays a field and no tag reaches the database. The fix is one selector entry, a millisecond time format for the 13-digit stamp, and a file-input test that prints line protocol so you can see the tag set before the database output runs. The worked example follows one CAM-style payload carrying station type 5.

The parser flattens nested names before tag matching #

With data_format = "json", Telegraf parses the whole document into one metric and joins every nesting level with underscores. A key listed in tag_keys is lifted out of the field set and emitted in the tag set; a key that matches nothing stays a field, which is exactly what the reported configuration produced.

JSON path in the payload Key the parser produces Example value
message.basic_container.station_type message_basic_container_station_type 5
message.station_id message_station_id 224
message.high_frequency_container.speed message_high_frequency_container_speed 500
timestamp timestamp 1605886891388

The timestamp keeps its plain name because it sits at the document root, with no nesting to flatten; that matters when you scope the parser, because the time key must stay reachable. One further documented parser behavior: listing an object or array key in tag_keys promotes every value beneath it to tags, so selecting the whole container object multiplies tag series instead of fixing the selector.

Set the selector to the flattened name #

Point tag_keys at the name the parser actually produces. The addresses below are the local defaults for a Mosquitto broker and the version 1.x database output; substitute the endpoints and the database name your deployment uses.

[[inputs.mqtt_consumer]]
  servers = ["tcp://127.0.0.1:1883"]
  topics = ["vehicles/cam/#"]
  data_format = "json"
  tag_keys = ["message_basic_container_station_type"]
  json_time_key = "timestamp"
  json_time_format = "unix_ms"

[[outputs.influxdb]]
  urls = ["http://127.0.0.1:8086"]
  database = "v2x_manager"

Two details in that block decide whether points land at all.

Timestamp format. The stamp 1605886891388 has 13 digits, so it counts milliseconds: 1605886891388 ms = 1605886891.388 s, which is 20 November 2020, 15:41:31 UTC. The value unix_ms reads it that way; plain unix reads seconds and would set the point roughly 50,000 years from any real measurement, where the write fails or no retention policy reaches it. A feed that stamps seconds — 10 digits for current dates — keeps plain unix.

Payload time versus receive time. json_time_key makes the point time come from the payload itself. Where the pipeline must also keep the arrival time, the article on keeping both the arrival time and the payload timestamp in Telegraf shows how to preserve the two.

Quotation marks. Configuration pasted through chat or a wiki often carries typographic curly quotes; the configuration parser accepts only straight ASCII quotes, so replace any curly character in servers, topics, data_format and tag_keys before testing.

If the database schema expects a tag literally named station_type, prove ingestion with the flattened selector first, then rename the tag in a separate processing stage between parser and output.

Prove the parse without the broker #

Separate the parser from the transport. A temporary file input with the same parser settings reproduces the exact parse of a captured payload, with no broker, subscription or network involved:

  1. Save one complete payload as plain JSON, with no log lines around it.
  2. Add an inputs.file block for that file with data_format = "json" and the same tag_keys entry.
  3. Set json_time_key and json_time_format as above.
  4. Replace the database output with a file output that writes Influx line protocol.
  5. Run this test configuration on its own, so live system inputs cannot flood the result.
  6. Read the printed line.

A selector that works puts the tag before the first space, next to the measurement name; a value after the first space is a field:

mqtt_consumer,message_basic_container_station_type=5 message_station_id=224,message_high_frequency_container_speed=500 1605886891388000000

If the line instead reads

mqtt_consumer message_basic_container_station_type=5,message_station_id=224,message_high_frequency_container_speed=500 1605886891388000000

the value is still a field and tag_keys still does not match the flattened name. These two lines are illustrative output for the example payload, not measured results from a broker. Running live, debug = true under the agent section prints every metric as line protocol and shows the same distinction on the real subscription.

The broker path is a separate checkpoint #

A parser that passes the file test can still fail on the live subscription, and the measurements already visible in the database prove less than they seem. cpu, disk, mem, processes and system come from the system input plugins: they show that the output accepts writes, not that the MQTT consumer received or parsed a payload. Check the transport in its own right: confirm the broker holds a message on the exact subscribed filter, watch the debug log for the subscription and the received payload, then select the database the output block names before running show measurements, because a differently named database lists none of your data. When the consumer connects but no measurement appears at all, that is a different fault, covered step by step in the article on a Telegraf MQTT consumer that connects but writes no data to InfluxDB.

Scope to a subtree only when the subtree is the point #

Scoping the parser with json_query changes the names tag matching sees. Query the basic-container subtree and station_type becomes a top-level key of the parsed document, so the short selector tag_keys = ["station_type"] would match — but the metric then loses everything outside that subtree: the high-frequency container, the station identifier and the root-level timestamp key, which breaks the time setting above. Use the full-document parse when the existing database expects the whole payload; use json_query only for a measurement deliberately limited to one subtree. After parsing, the fieldpass and taginclude filters trim what is written by name — filters on names the parser already produced, described in the article on filtering MQTT fields in Telegraf before they reach InfluxDB. Neither filter can rescue a selector that never matched.

Decide by symptom #

Symptom Cause Fix
Fields are written, the tag is absent tag_keys names the dotted path set it to the flattened station-type key
Points land far off, or the write fails millisecond stamp parsed as seconds unix_ms for 13-digit stamps
The selector matches nothing on a top-level key curly quotes or a spelling mismatch straight ASCII quotes; compare with debug line protocol
The schema requires the short tag name the flattened selector name prove ingestion, then rename in a processing stage
Only one subtree should become the metric deliberate scoping json_query, accepting the loss of sibling keys and the root timestamp

For the reported payload the decision is the first row: point the selector at the flattened key, restart Telegraf, and the next payload writes with the station type in the tag set. Keep unix_ms while the feed sends 13-digit stamps. Filters, subtree scoping and renaming come after ingestion is proven, never instead of it.

Questions asked

Will points written before the fix gain the tag afterwards?

No. Each point is written with the schema it had at write time, and changing tag_keys does not rewrite stored history. Points written before the selector change remain field-only. The tag appears on the first payload Telegraf parses after restarting with the corrected configuration, so confirm the fix by querying a window that starts after that restart rather than the whole measurement.

How do I tell a millisecond timestamp from a seconds timestamp?

Count the digits against the epoch scale. Seconds values for current dates have 10 digits, about 1.6 billion; milliseconds have 13, about 1.6 trillion. A 13-digit key needs unix_ms; a 10-digit key needs plain unix. Some devices send microseconds with 16 digits, which needs the parser's microsecond format instead.

Does telegraf.conf reload when I save the file?

No. The file is read at process start, so apply a corrected tag_keys or time format by restarting the Telegraf service. With debug output enabled, the next payload prints with the tag in the tag set, which is the fastest confirmation that the new configuration loaded. If the service fails to start after an edit, a syntax error is the first thing to check.

What name does the measurement carry in the test output?

With no override configured, Telegraf names the metric after the input plugin: mqtt_consumer on the live subscription, file in a file-input test, which is why the debug sample from a file test starts with the file name. Set a name override only when you need one stable measurement name across tests and production.

Keep reading

  • How-to

    Keep both arrival time and payload timestamp in Telegraf

    Setting json_time_key replaces the metric timestamp with the payload time, so the arrival time is lost before any output sees it. Drop that option, keep the payload timestamp as a field, and use a Starlark processor that returns two metrics: one point keeps the arrival timestamp with the payload time as a field, the clone takes the payload value as its timestamp.

    Edge computing and IIoT

  • Troubleshooting

    Why a Node-RED timestamp creates a new InfluxDB tag

    Your timestamp is landing in the tag set, and InfluxDB line protocol gives every distinct tag value its own series, so each sample is written as a separate one-point series and the trend is blank. Put the epoch time in the point's time slot with the node's time precision set to match, and keep tags to identifiers that repeat, such as machine and sensor.

    Edge computing and IIoT

  • How-to

    Turn an MQTT topic into InfluxDB fields with Telegraf

    Telegraf reads /temperature/boiler/room3 with payload 21.7 as a metric carrying a topic tag and a value field. A five-processor chain (regex, converter, regex, regex, pivot) renames the measurement, builds location=room3 and pivots the middle segment into the field key; one Starlark processor does the same in eight lines. Either way the output line is temperature,location=room3 boiler=21.7 with the original timestamp.

    Edge computing and IIoT

Why historian CSV timestamps show only the date in Excel