> ## Documentation Index
> Fetch the complete documentation index at: https://docs.startree.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON Value Message Decoder

> Store each JSON message as one value, so payload keys can't overwrite header columns and non-object messages aren't dropped.

The JSON value decoder stores the whole message body as a single JSON value in a field called `__value`. It doesn't split the body into one field per top-level key, so you choose every column explicitly with ingestion transforms.

### Class

`ai.startree.pinot.plugin.inputformat.json.JsonValueMessageDecoder`

## When to use it

Use it when the message body isn't designed around your table schema. The standard [JSON decoder](https://docs.startree.ai/corecapabilities/ingestdata/adv-concepts/realtime/decoders/json.md) maps each top-level key to the column with the same name, which causes two problems:

* **Body keys overwrite other columns.** If you fill a column such as `source` from a Kafka header and the body also has a `source` key, the column holds the body's value. A transform doesn't run on a column that already has a value, so the header value is lost.
* **Messages that aren't JSON objects are dropped.** Strings, numbers, booleans, arrays and `null` have no keys to split, so the row is skipped along with its headers.

| Message | JSON decoder | JSON value decoder |
| - | - | - |
| `{"source":"internal","state":"IDLE"}` | `source` = `internal`; the header value is lost | `payload` = the whole message; `source` = the header value |
| `"IDLE"`, `42`, `true`, `[1,2]`, `null` | Dropped | Stored as-is |
| `{"a":1} junk` | Stored as `{"a":1}`; the trailing text is silently lost | Rejected |
| Plain text, or an empty message | Dropped | Dropped |

A message must be exactly one JSON value. Plain text, trailing content, and empty or truncated messages are rejected like any malformed message, and consumption continues.

## Configuration example

Set the decoder in `streamConfigs`, then add one transform per column. Each transform reads `__value`, a column that stores it, a header, the key, or metadata.

```json theme={null}
{
  "tableIndexConfig": {
    "streamConfigs": {
      "streamType": "kafka",
      "stream.kafka.topic.name": "events_topic",
      "stream.kafka.broker.list": "broker:9092",
      "stream.kafka.decoder.class.name": "ai.startree.pinot.plugin.inputformat.json.JsonValueMessageDecoder",
      "stream.kafka.metadata.populate": "true"
    }
  },
  "ingestionConfig": {
    "transformConfigs": [
      { "columnName": "payload", "transformFunction": "\"__value\"" },
      { "columnName": "source", "transformFunction": "fromUtf8(\"__header$source\")" },
      { "columnName": "state", "transformFunction": "jsonPathString(payload, '$.state', null)" }
    ]
  }
}
```

* `stream.kafka.metadata.populate` is only needed when a column reads a header (`__header$<name>`) or metadata (`__metadata$<name>`).
* Make the column that stores `__value` a `JSON` column. You can then query it with `JSON_EXTRACT_SCALAR` and `JSON_MATCH`, add a JSON index, and add derived columns later. A segment reload fills new derived columns from the stored value.

### Message formats

The decoder reads text JSON by default. Set `stream.kafka.decoder.prop.jsonFormat` for other encodings:

| Value | Format |
| - | - |
| `TEXT` | Plain JSON text (default) |
| `SMILE` | Smile binary JSON |
| `CBOR` | CBOR |
| `POSTGRES_JSONB` | PostgreSQL JSONB |
| `AUTO` | Detect the format per message |

`SQLITE_JSONB` isn't supported.

## Querying the stored value

```sql theme={null}
-- Read a field from the stored message
SELECT JSON_EXTRACT_SCALAR(payload, '$.state', 'STRING', 'none') FROM events

-- Match a message whose whole body is the string "IDLE"
SELECT * FROM events WHERE JSON_MATCH(payload, '"$" = ''IDLE''')
```

## Table config validation

StarTree checks the table config when you create or update the table, and rejects it if a column would be left empty or read the wrong data:

* A schema column has no transform.
* A transform, filter or ingestion aggregation reads a top-level body key such as `state`. Read it from `__value` with `jsonPathString` instead.
* A schema column is named `__value`.
* A table reads `__value` but uses a different decoder. No other decoder fills `__value`.
* Streams of the same table use different decoders.
* The table sets `schemaConformingTransformerConfig`, `complexTypeConfig`, a custom `recordExtractorClass`, or `jsonFormat` set to `SQLITE_JSONB`.

## Performance

Decoding takes longer than with the standard JSON decoder because each message is parsed, written back as JSON text, then parsed again by the `JSON` column. In StarTree's tests, a 142-byte message took about 850 ns against about 525 ns, and a 100 KB message about 535 µs against about 430 µs.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.