parsejson
Parses a JSON string in a text field and extracts key-value pairs as fields.
Command properties
| Item | Description |
|---|---|
| Command type | Processing query |
| Required permission | None |
| License usage | N/A |
| Parallel execution | Supported |
| Distributed execution | Not supported |
Syntax
Options
field=FIELD- Name of the field to parse (default:
line) overlay={t|f}- When set to
t, preserves the original record's fields and overlays the parsed fields onto them. When omitted, outputs only the parsed fields. (default:f) flatten={t|f}- When set to
t, flattens nested JSON objects. Nested keys are joined with an underscore (_). Array elements are joined with their index number. Whenflatten=t, the default value ofcutoffchanges to5000. (default:f) cutoff=N- Maximum number of characters to parse. When
0, parses the entire string without limit. Whenflatten=t, the default changes to5000. (default:0) lenient={t|f}- When set to
t, recovers and parses incomplete JSON truncated bycutoff. (default:f)
Error codes
Parse errors
| Error code | Message | Description |
|---|---|---|
| 21100 | The cutoff value [cutoff] must be an integer of 0 or more. | The cutoff value is less than 0 |
Description
The parsejson command parses the JSON string in the specified field and extracts key-value pairs as fields.
Using the flatten option flattens nested JSON objects. For example, {"a":{"b":1}} results in the field a_b with a value of 1. Arrays are flattened using index numbers. For example, {"x":["a","b"]} creates the fields x_0 and x_1.
Using cutoff and lenient together allows you to parse only up to a specified size of a large JSON string and automatically recover the truncated portion.
Examples
-
Parsing a JSON string
json "{'line': '{\"src_ip\": \"192.0.2.1\", \"dst_port\": 80}'}" | parsejsonParses the JSON string in the
linefield and creates thesrc_ipanddst_portfields. -
Flattening nested JSON
json "{'line': '{\"parent\":{\"child\":1, \"sibling\":2}}'}" | parsejson flatten=tCreates the
parent_childandparent_siblingfields. -
Flattening an array
json "{'line': '{\"items\":[\"a\",\"b\",\"c\"]}'}" | parsejson flatten=tCreates the
items_0,items_1, anditems_2fields. -
Parsing only part of a large JSON string
json "{'line': '{\"company\":\"Logpresso\"}'}" | parsejson cutoff=12 lenient=tParses the JSON string up to 12 characters and recovers the truncated portion for parsing.