spath
Contents
spath
The spath command--the "s" stands for Splunk (or structured) -- provides a straightforward means for extracting information from structured data formats, XML and JSON.
You can also use the eval command's spath() function. For more information, see the Functions for eval and where.
Synopsis
Extracts values from structured data (XML or JSON) and stores them in a field or fields.
Syntax
spath [input=<field>] [output=<field>] [path=<datapath> | <datapath>]
Optional arguments
- input
- Syntax: input=<field>
- Description: The field to read in and extract values. Defaults to _raw.
- output
- Syntax: output=<field>
- Description: If specified, the value extracted from the path is written to this field name.
- path
- Syntax: path=<datapath> | <datapath>
- Description: The location path to the value that you want to extract. If you don't use the
pathargument, the first unlabeled argument will be used as a path. A location path is composed of one or more location steps, separated by periods; for example 'foo.bar.baz'. A location step is composed of a field name and an optional index surrounded by curly brackets. The index can be an integer, to refer to the data's position in an array (this will differ between JSON and XML), or a string, to refer to an XML attribute. If the index refers to an XML attribute, specify the attribute name with an @ symbol. If you don't specify an output argument, this path becomes the field name for the extracted value.
Description
A location path contains one or more location steps, each of which has a context that is specified by the location steps that precede it. The context for the top-level location step is implicitly the top-level node of the entire XML or JSON document.
The location step is composed of a field name and an optional array index indicated by curly brackets around an integer or a string. Array indices mean different things in XML and JSON. For example, in JSON, foo.bar{3} refers to the third element of the bar child of the foo element. In XML, this same path refers to the third bar child of foo.
The spath command lets you use wildcards to take the place of an array index in JSON. Now, you can use the location path entities.hashtags{}.text to get the text for all of the hashtags, as opposed to specifying entities.hashtags{0}.text, entities.hashtags{1}.text, etc. The referenced path, here entities.hashtags has to refer to an array for this to make sense (otherwise you get an error, just like with regular array indices).
This also works with XML; for example, catalog.book and catalog.book{} are equivalent (both will get you all the books in the catalog).
When called with no path argument, spath runs in "auto-extract" mode, where it finds and extracts all the fields from the first 5000 characters in the input field (which defaults to _raw if another input source isn't specified). If a path is provided, the value of this path is extracted to a field named by the path or to a field specified by the output argument (if it is provided).
Examples
Example 1: GitHub
As an administrator of a number of large git repositories, I want to:
- see who has committed the most changes and to which repository
- produce a list of the commits submitted for each user
I set up Splunk to track all the post-commit JSON information, then use spath to extract fields that I call repository, commit_author, and commit_id:
... | spath output=repository path=repository.url... | spath output=commit_author path=commits.author.name... | spath output=commit_id path=commits.idNow, if I want to see who has committed the most changes to a repository, I can run the search:
... | top commit_author by repositoryand, to see the list of commits by each user:
... | stats values(commit_id) by commit_authorExample 2: Extract a subset of an attribute
This example shows how to extract values from XML attributes and elements.
<vendorProductSet vendorID="2">
<product productID="17" units="mm" >
<prodName nameGroup="custom">
<locName locale="all">APLI 01209</locName>
</prodName>
<desc descGroup="custom">
<locDesc locale="es">Precios</locDesc>
<locDesc locale="fr">Prix</locDesc>
<locDesc locale="de">Preise</locDesc>
<locDesc locale="ca">Preus</locDesc>
<locDesc locale="pt">Preços</locDesc>
</desc>
</product>
To extract the values of the locDesc elements (Precios, Prix, Preise, etc.), use:
... | spath output=locDesc path=vendorProductSet.product.desc.locDescTo extract the value of the locale attribute (es, fr, de, etc.), use:
... | spath output=locDesc.locale path=vendorProductSet.product.desc.locDesc{@locale}To extract the attribute of the 4th locDesc (ca), use:
... | spath path=vendorProductSet.product.desc.locDesc{4}{@locale}More examples
Example 1:
... | spath output=myfield path=foo.bar... | spath output=myfield path=foo{1}... | spath output=myfield path=foo.bar{7}.bazExample 2:
... | spath output=author path=book{@author}See also
extract, kvform, multikv, regex, rex, xmlkv, xpath
Answers
Have questions? Visit Splunk Answers and see what questions and answers the Splunk community has using the spath command.
This documentation applies to the following versions of Splunk: 4.3 , 4.3.1 , 4.3.2 View the Article History for its revisions.