spath
import { Aside } from ‘@astrojs/starlight/components’;
The spath command extracts fields from structured JSON data stored in a text field. It operates in two modes:
- Path-based mode — When
pathis specified, extracts a single value at the given JSON path. - Auto-extract mode — When
pathis omitted, extracts all fields from the JSON into a map.
This is ideal for semi-structured log bodies that contain JSON payloads — you can extract and query nested fields without re-indexing.
Syntax
Section titled “Syntax”spath input=<field> [output=<field>] [[path=]<json-path>]Arguments
Section titled “Arguments”Required
Section titled “Required”| Argument | Description |
|---|---|
input=<field> | The field containing JSON data to parse. Must be a string field. |
Optional
Section titled “Optional”| Argument | Default | Description |
|---|---|---|
output=<field> | Value of path (path mode) or input (auto-extract) | Destination field for the extracted data. |
path=<json-path> | — | The JSON path identifying data to extract. When omitted, runs in auto-extract mode. The path= keyword is optional; you can specify the path as a positional argument. |
JSON path syntax
Section titled “JSON path syntax”| Syntax | Description | Example |
|---|---|---|
field | Top-level field | status |
parent.child | Dot notation for nested fields | error.message |
list{0} | Array element by index | tags{0} |
list{} | All array elements | items{} |
"['special.name']" | Escaped field names with dots or spaces | "['a.b.c']" |
Usage notes
Section titled “Usage notes”- The
spathcommand always returns extracted values as strings. Useevalwithcast()to convert to numeric types for aggregation. - The input field must contain a valid JSON string. Struct or map fields from the index schema cannot be used directly — you must first convert them to a string representation.
- In auto-extract mode, nested objects produce dotted keys (
user.name), arrays produce{}suffix keys (tags{}), and all values are stringified. - Empty JSON objects (
{}) return an empty map. Malformed JSON returns partial results from any fields parsed before the error. - In auto-extract mode, access individual values via dotted path navigation on the output field (e.g.,
doc.user.name). For keys containing{}, use backtick quoting.
Examples
Section titled “Examples”Extract a field from a JSON string
Section titled “Extract a field from a JSON string”Extract the status field from a JSON string. This example uses eval to create a JSON string for demonstration, but in practice you would use this on a body field that already contains JSON:
source=logs-otel-v1*| head 1| eval jsonStr = '{"status": 200, "service": "frontend", "latency": 45}'| spath input=jsonStr path=status output=httpStatus| httpStatus |
|---|
| 200 |
Extract nested object fields
Section titled “Extract nested object fields”Traverse multiple levels of nesting using dot notation to extract deeply nested values:
source=logs-otel-v1*| head 1| eval jsonStr = '{"error": {"type": "timeout", "message": "upstream timed out"}}'| spath input=jsonStr path=error.message output=errorMsg| errorMsg |
|---|
| upstream timed out |
Extract array elements
Section titled “Extract array elements”Extract the first element and all elements from an array within JSON data:
source=logs-otel-v1*| head 1| eval jsonStr = '{"tags": ["frontend", "v2", "canary"]}'| spath input=jsonStr path=tags{0} output=firstTag| spath input=jsonStr path=tags{} output=allTags| firstTag | allTags |
|---|---|
| frontend | [“frontend”,“v2”,“canary”] |
Cast extracted values for aggregation
Section titled “Cast extracted values for aggregation”Extracted values are strings. Cast them before performing numeric operations:
source=logs-otel-v1*| head 1| eval jsonStr = '{"status": 200, "service": "frontend", "latency": 45}'| spath input=jsonStr path=latency output=latency| eval latency = cast(latency as double)Auto-extract all fields from JSON
Section titled “Auto-extract all fields from JSON”Extract all fields from a JSON string into a map, then access individual values:
source=logs-otel-v1*| head 1| eval jsonStr = '{"status": 200, "service": "frontend"}'| spath input=jsonStr output=parsed| parsed |
|---|
| {service: frontend, status: 200} |
Extended examples
Section titled “Extended examples”Extract multiple error fields from a JSON payload
Section titled “Extract multiple error fields from a JSON payload”Chain multiple spath commands to extract several fields from a nested error payload:
source=logs-otel-v1*| head 1| eval jsonStr = '{"error": {"type": "timeout", "message": "upstream timed out", "code": 504}}'| spath input=jsonStr path=error.type output=errorType| spath input=jsonStr path=error.message output=errorMsg| spath input=jsonStr path=error.code output=errorCode| errorType | errorMsg | errorCode |
|---|---|---|
| timeout | upstream timed out | 504 |