Troubleshooting
Why Telegraf stores only one value of a JSON array in InfluxDB
Short answer
InfluxDB line protocol takes only scalar field values — float, integer, string, boolean — so a json_v2 object write puts every array element under one field key at one timestamp and the last element replaces the rest. Expand fixed arrays into named scalar fields or write one point per element with an index tag, and fix the parser blocks first.
On this page
An InfluxDB field cannot hold an array. Line protocol, the text format Telegraf writes to the bucket, accepts only scalar field values, so a JSON array has no single field to land in. A json_v2 object block pointed at the whole message emits every element under the same field key at the same timestamp, and InfluxDB keeps the newest write, so the last element of the array is all that reaches the bucket. The fix is a storage decision per array, not a query change: expand fixed-length arrays into named scalar fields (speed.0 becomes speed_1 as a float) and write one point per element with an index tag for lists whose length changes. Five configuration errors in this setup block even the correct shape, and they come first.
Why the array collapses to its last element #
Line protocol carries a measurement, a tag set, a field set and one timestamp per point, and its field values are scalars: float, integer, string or boolean. There is no array type and no nested-object type. That is a property of the line protocol format itself, not of one Telegraf release, so every output that speaks it is bound by the same limit.
The collapse then follows from InfluxDB's duplicate-point rule. An object block whose path selects the whole document (@this) walks every key in the payload, and at speed it emits one write per element, all under the same field key and all carrying the message's single timestamp. InfluxDB treats a point with the same measurement, tag set, field key and timestamp as a duplicate and keeps the newest field value, so of the four writes — 0.0, 1.0, 2.0, 4.0 — the last element is the one that remains.
The Flux pivot in the query only reshapes columns for display. It deleted nothing and hid nothing; the other elements were gone before any query ran, which is why raw points, not pivoted tables, are the evidence to read first. How such an array behaves at each hop between broker and bucket is traced step by step in the guide on how Telegraf MQTT arrays reach InfluxDB intact.
Fix these five configuration errors first #
- Parser block attached to the wrong input. The tag block is written as
[[inputs.file.json_v2.tag]]inside a configuration whose only input isinputs.mqtt_consumer. That block belongs to a file input, so the vehicle tag never reaches the parser that receives these messages; every tag and field block must hang under[[inputs.mqtt_consumer.json_v2]]. - Root selector written with spaces. The object block sets its path with a space on each side of
@this, and a padded selector does not match the document root. Write it as@thiswith no surrounding spaces, or skip the whole-document object block and declare each field path explicitly, which is the pattern used below. - The payload is not valid JSON. The
timemember carries the unevaluated Python expressionstr(datetime.datetime.utcnow().strftime("%Y-%m-%dT %H:%M%SZ"))instead of its result, and several string values inpointsuse typographic quotation marks, which JSON rejects as delimiters. Evaluate the timestamp in the publisher, serialize with ordinary double quotes, and validate the captured bytes with a JSON validator before blaming the parser. - Event time left implicit.
left_behind_atequals 1634607917.8445928 seconds since the epoch, which is 2021-10-19T01:45:17.844Z in UTC, but the parser uses it as the point timestamp only when its timestamp settings name that field and its format. Without that, points carry receive time, and a query window set for event time finds nothing. - Publishers that overwrite each other. Two servers running the same configuration produce the same measurement and the same field keys, so writes landing on the same timestamp replace one another. Add a tag that separates the publishers — the vehicle name in this payload — before comparing data across units.
The corrected parser fragment for the speed array #
[[inputs.mqtt_consumer]]
servers = ["tcp://172.29.60.9:1883"]
topics = ["vdcs/vehicle-cmd/+"]
data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
measurement_name = "vehicle_status"
[[inputs.mqtt_consumer.json_v2.tag]]
path = "name"
rename = "vehicle"
[[inputs.mqtt_consumer.json_v2.field]]
path = "speed.0"
rename = "speed_1"
type = "float"
[[inputs.mqtt_consumer.json_v2.field]]
path = "speed.1"
rename = "speed_2"
type = "float"
[[inputs.mqtt_consumer.json_v2.field]]
path = "speed.2"
rename = "speed_3"
type = "float"
[[inputs.mqtt_consumer.json_v2.field]]
path = "speed.3"
rename = "speed_4"
type = "float"
The fragment keeps broker and topic settings in the enclosing input and declares only what the objective needs: the vehicle tag and the four speed fields. The plain scalars — x, y, distance, gy, current_throttle, left_behind_at — are added the same way, one field block each. If whole-document extraction is kept instead, excluded_keys names the members to drop, and explicit field blocks override the arrays. Declaring one block per field is the same pattern used when configuring Telegraf MQTT JSON fields from a LoRaWAN feed. If the json_v2 block structure itself is new, the walkthrough on parsing MQTT JSON with Telegraf for InfluxDB starts from an empty configuration.
Choose the storage shape per array #
| Member | Shape in this payload | Stored as |
|---|---|---|
name |
one string | tag vehicle |
speed |
4 elements | 4 float fields speed_1 to speed_4, paths speed.0 to speed.3 |
angle |
8 elements | excluded here; if kept and always 8 long, 8 fields; if the count varies, one point per element with an index tag |
odometer |
8 elements | excluded here; same rule as angle |
points |
3 objects of 3 members each | one point per object, element index as a tag, type, x, y as fields |
x, y, distance, gy, current_throttle, left_behind_at |
one scalar each | 6 float fields |
time |
invalid JSON as sent | fixed at the publisher; then a stored string or the parser's timestamp source |
The rule behind the table: an array may become fields only while its length is the same in every message and each position keeps the same meaning. Miss either condition and the only correct shape is one point per element, with the element index in the tag set. For points, keep each object's members together on that element's point; collecting x from every object into one field key would recreate the collapse. Nesting like this is also what leaves SCADA points blank when a client expects flat fields, as covered in the article on SCADA MQTT points that go blank with nested JSON payloads.
Worked example: the A001 message with a four-element speed array #
Inputs, all from the message the consumer receives on vdcs/vehicle-cmd/+, quoted as published:
speed= [0.0, 1.0, 2.0, 4.0] — 4 elementsangle— 8 elements;odometer— 8 elementspoints— 3 objects of 3 members each (type,x,y)name= "A001", plus 6 plain scalars:x,y,distance,gy,current_throttle,left_behind_at
Rule in words: one field holds one scalar; a fixed-length array whose positions keep their meaning expands into one float field per element through indexed paths; a list whose length can change becomes one point per element with the index as a tag; excluded keys are dropped.
Arithmetic:
speed: 4 elements → pathsspeed.0,speed.1,speed.2,speed.3→ 4 float fields.- Plain scalars kept: 6, so the main point carries 4 + 6 = 10 float fields.
points: 3 × 3 = 9 scalar members → 3 additional points with 3 fields each, if kept.angleandodometer, excluded in this objective: 8 + 8 = 16 published values dropped.- Left under the whole-document object block, the 4 speed elements would share one field key at one timestamp: 4 writes, 1 survivor.
Result: one point per message on measurement vehicle_status tagged vehicle=A001, with fields speed_1=0.0, speed_2=1.0, speed_3=2.0, speed_4=4.0 among its 10 fields, plus 3 object points if points is kept.
Decision and threshold: while the element count is fixed — speed stays 4 in every message — fields are the right shape. The first message whose list comes back longer or shorter disqualifies field expansion for that array; the points list holds 3 objects now and can hold a different count in the next message, so it takes one point per object with an index tag. A vector whose positions change meaning between messages fails the same test.
Verify the write before judging the query #
Replay one captured message through the corrected configuration with the output pointed at a file, and read the line protocol Telegraf actually emits: the main point should appear once, tagged vehicle=A001, with ten fields. Then query the raw points before any pivot:
from(bucket: "vehicle_status")
|> range(start: 2021-10-19T00:00:00Z, stop: 2021-10-19T02:00:00Z)
|> filter(fn: (r) => r._measurement == "vehicle_status")
The range covers this message's event-time instant, 2021-10-19T01:45:17Z; with receive-time stamps, any window around the replay works. Expect four rows with _field values speed_1 through speed_4 at one timestamp, holding 0.0, 1.0, 2.0 and 4.0 as floats. After the pivot — row key on _time, column key on _field, value column on _value — the same data reads as one row with four speed columns. If those rows exist but a dashboard still shows one value, the fault sits in the client's aggregation window, not in the write path. If the consumer connects and nothing at all reaches the bucket, that is a different fault, covered in the article on a Telegraf MQTT consumer that connects but writes no data to InfluxDB.
One bench note: the example addresses keep plain tcp:// transport for an isolated test bench; on any shared network, move the broker to its authenticated, encrypted listener and keep tokens and credentials out of pasted configurations.
Questions asked
Does the Flux pivot delete the other array values?
No. The pivot only reshapes query output into columns. The other elements were already absent from the bucket, because every element of the array was written to the same field key at the same timestamp and InfluxDB kept the newest write. Query the raw points before the pivot and you will find one value per field key, not a truncated table.
Can the whole JSON message be stored as one string field?
Yes, by serializing the document to a JSON string at the publisher and writing it under a string field. That preserves every array for later retrieval, but the numbers inside cannot be aggregated or filtered natively; the string has to be parsed again downstream. Store scalar fields alongside it for every value a query needs.
Why did retyping the points members from numbers to strings not help?
Changing a member's type inside a nested object does not turn that object into a supported field type; line protocol still has no object or array field. A string sent under a field key that already holds numbers is rejected as a type conflict. Split each object onto its own point with scalar fields instead.
How is element order kept when one point per element is used?
Put the element index in the tag set, so element 0 of a four-element array becomes its own series with index 0. Points in different series never overwrite each other, and queries can sort on that tag. Never rely on write order, because same-timestamp writes to one series replace each other.
Do several publishers each need their own measurement?
No, one measurement is fine when every publisher carries a distinguishing tag, such as the vehicle name. Telegraf adds a host tag by default; the agent's omit_hostname setting removes it when it is true. Without a separating tag, two units writing the same field keys at the same timestamp silently replace each other's values.