Skip to content

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 path is specified, extracts a single value at the given JSON path.
  • Auto-extract mode — When path is 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.

spath input=<field> [output=<field>] [[path=]<json-path>]
ArgumentDescription
input=<field>The field containing JSON data to parse. Must be a string field.
ArgumentDefaultDescription
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.
SyntaxDescriptionExample
fieldTop-level fieldstatus
parent.childDot notation for nested fieldserror.message
list{0}Array element by indextags{0}
list{}All array elementsitems{}
"['special.name']"Escaped field names with dots or spaces"['a.b.c']"
  • The spath command always returns extracted values as strings. Use eval with cast() 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.

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

Try in playground →

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

Try in playground →

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
firstTagallTags
frontend[“frontend”,“v2”,“canary”]

Try in playground →

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)

Try in playground →

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}

Try in playground →

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
errorTypeerrorMsgerrorCode
timeoutupstream timed out504

Try in playground →

  • parse — extract fields using regex named capture groups
  • grok — extract fields using grok patterns
  • rex — regex extraction with sed-mode substitution
  • eval — create computed fields and type conversions