IMD SolutionComputing LibraryShop main store

Troubleshooting

Why an MQTT client logs an invalid JSON warning

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

An MQTT client logs an invalid JSON warning because the payload it received is not the JSON document its parser requires — often a bare number such as 215, which is valid JSON text but not the JSON object the client expects. Confirm the raw payload bytes, then publish a JSON object in the payload and leave the topic path alone.

On this page
  1. Read the warning as a parser message
  2. A subscriber that accepts anything is not proof
  3. Why a bare number fails a JSON client
  4. Worked example: the 215 payload on revpi8143/io/temp_heater
  5. Reformat the payload without touching the topic
  6. When the client vendor owes the fix
  7. Questions asked

An MQTT client that logs an invalid JSON warning has received payload bytes its JSON parser cannot place. It is not a broker fault and not a topic problem. In the plant case behind this article the broker delivered every message on time and a plain subscriber printed them cleanly; only the JSON-parsing client filled its event log. The payload there was a bare number, 215, where the client's parser demanded a JSON object, and the correction finally came from the client vendor. MQTT never reads the payload: it moves bytes, and JSON is a claim the publisher makes about them. Confirm the raw payload, publish the shape the parser requires, and leave the topic path alone.

Read the warning as a parser message #

The reported entry reads Unexpected token found while looking for the end of the MQTT payload. | Channel = '<channel>', Topic = '<topic>', Line = <line>, Col = <col>, with a possible cause of an unexpected token while looking for the end of the document and a suggested fix of reconfiguring the payload as valid JSON. The same entry appears in the event-log documentation of the Kepware MQTT client driver, which lists the cause as a payload that is not valid JSON. Read it as parser output, not as a delivery report: the line names the channel and the topic and points at the line and column of the token the parser rejected — the first byte it could not fit into the document shape it was told to expect.

A subscriber that accepts anything is not proof #

A verbose subscriber prints the two MQTT fields on one line:

revpi8143/io/temp_heater 215
revpi8143/io/temp_outside 227

revpi8143/io/temp_heater is the topic that routes the message; the digits after it are the entire payload. Node-RED took the same traffic without complaint, because a subscriber transports and prints arbitrary payload bytes and applies no document rules. That tolerance covers payloads that are not text at all, the case behind why Ignition Edge MQTT payloads look like unreadable bytes. The JSON client is the first program in the chain that applies JSON rules, so it is the first place a non-JSON payload can fail.

Why a bare number fails a JSON client #

RFC 8259, the JSON standard, defines a JSON text as one value with optional whitespace around it, so a bare number is a complete JSON document, and so are a quoted string, an object and an array. A client does not reject the payload because it is invalid JSON in the abstract; it rejects it because its parser is configured to read values out of a JSON object, which makes an opening brace the only acceptable first token. Nearby cases trip the same warning: a device publishing NaN for a failed reading, because the JSON grammar has no NaN token; a payload with one brace too many or a closing brace missing, reported arriving as valid-looking JSON and failing at the receiving parser; and stray characters after the last brace, the variant covered by fixing extra characters in a PLC MQTT JSON payload. Quotes around the digits are a different trap: they make the value a JSON string, which is why a quoted MQTT JSON value fails an analog point.

Worked example: the 215 payload on revpi8143/io/temp_heater #

Inputs, from the plant capture: the topic revpi8143/io/temp_heater; the payload 215, three bytes, 0x32 0x31 0x35 as ASCII; a client configured to parse every payload as a JSON object; log fields that report the line and column of the rejected token.

Rule: RFC 8259 accepts a bare number as a complete JSON text, but a parser that requires an object needs byte 1 to be an opening brace, 0x7B; any other non-whitespace byte is an unexpected token.

Arithmetic: byte 1 of 3 is 0x32, the digit 2. 0x32 is not 0x7B, so parsing stops at line 1, column 1, and bytes 2 and 3 are never read. The object form {"temp_heater":215} is 19 bytes: 1 for the opening brace, 13 for the key "temp_heater", 1 for the colon, 3 for the digits and 1 for the closing brace. Walking it: column 1 opens the object, columns 2 through 14 carry the key, column 15 the colon, columns 16 through 18 the number, column 19 closes the object at depth zero with no bytes left over.

Result: the same three digits fail at column 1 as a bare payload and parse clean as a 19-byte object.

Decision: if the client requires an object, publish the 19-byte object as the payload on the topic unchanged, and treat a first payload byte of 0x7B as the pass condition.

Reformat the payload without touching the topic #

The client's suggestion lands on the publisher, so work it in this order:

  1. Capture the bytes. Subscribe to the exact topic with a subscriber that can dump raw payloads and log the bytes rather than a formatted console line; record the first byte and the last byte.
  2. Classify the failure. Digits alone mean a bare number; NaN or an empty payload means a value no JSON parser can accept; readable text after the closing brace means trailing characters; a lone brace or an extra one means unbalanced braces.
  3. Wrap the value at the publisher. Publish the object form with the topic unchanged. Braces never go into the topic path: the topic is a routing string, the payload is the data, and MQTT keeps them separate fields even when one console line shows them together.
  4. Bridge what you cannot change. If the device firmware cannot publish objects, run a small bridge that subscribes to the raw topic, wraps each value and republishes it to a second topic, then point the client at the second topic. Keep the bridge out of any control path — the Node-RED IO data MQTT setup that keeps publish and control separate shows the same split.
  5. Replay and verify. Feed the captured payloads back through, confirm the client's values update and the warning count stays flat across a shift rather than across one message.

When the client vendor owes the fix #

In the reported case the publisher could not be changed and the correction came from the client vendor as a workaround or fix; the exact build and change were never published, and a later report tied the same warnings to Kepware version 6.15. Treat both as reported symptoms, not as a documented defect in every installation. If your captured payload is verifiably valid JSON and the client still warns, escalate with the exact warning line, the client version, the channel and topic, the captured bytes and a replay that reproduces the warning on demand. After the vendor's change, replay the same set and confirm the values update while the invalid-JSON count stays flat.

Questions asked

Is a bare number like 215 valid JSON?

Yes. RFC 8259 defines a JSON text as a single value with optional whitespace, so 215, a quoted string, true, false, null, an array and an object are each complete JSON documents. An industrial MQTT client can still reject the bare number when its parser is configured to read values out of a JSON object, because the first token must then be an opening brace.

Why does the warning fill the log while my values still update?

The client logs one warning for every payload it cannot parse, so a busy topic produces thousands of entries while values the driver can still extract keep updating. The count is a per-message parser result, not a delivery failure, and the log growth alone is a reason to fix the payload quickly.

Can I put the JSON object into the topic?

No. MQTT keeps the topic and the payload as separate fields: the topic is a routing string and the payload is the data. A verbose subscriber prints them on one line, which invites the confusion, but keys and braces belong in the payload and the topic stays a plain path such as revpi8143/io/temp_heater.

Keep reading

  • How-to

    Set up the MQTT Client SL library on a CODESYS controller

    MQTT Client SL is part of the CODESYS IIoT Libraries SL package. Verify the exact controller runtime and its license, pin the library version, batch JSON per asset, and set the controller clock before opening a TLS session.

    Edge computing and IIoT

  • Explainer

    Shrinking a Sparkplug JSON payload on a narrow link

    Casting the JSON to bytes changes the Sparkplug datatype tag, not the size: String and Bytes metrics both ride one protobuf length-delimited field with the same length prefix. The example's ~700-byte object minifies to ~400 bytes and shrinks further as typed metrics with aliases; with a 1460-byte IPv4 segment limit, the gain is bytes on the link, not packets.

    Edge computing and IIoT

  • Explainer

    What a Node-RED simulator for OPC UA and MQTT data teaches

    A simulator stands in for the PLC, SCADA server or broker you cannot connect to yet, so you can build and prove the whole Node-RED acquisition path — input node, debug, one JSON decode, normalization, dashboard. The rule it teaches: decode each payload exactly once — set the MQTT input to a parsed JSON object and drop the JSON node, or keep the raw text and decode with one JSON node.

    Edge computing and IIoT