Getting Data In

Spath with path set by field

abella
Engager

I have a JSON object that has IP addresses as keys like the following

{
     "10.10.0.1" : { ... },
     "10.10.1.1" : { ... }
}

I'm passing spath a client IP (say client_ip="10.10.0.1" for this example), and want to access it as follows:

| spath input=<IP JSON object> output=out path=<client_ip>{}

How would I go about phrasing this in Splunkish? I'm essentially looking to dereference my client_ip field.

Tags (3)

dmr195
Communicator

Your problem is particularly tricky because your keys are IP addresses, and hence contain dots.

I had a similar problem to you and managed to solve my case, but when I tried to apply the same solution to your data it didn't work, and the extra complication is the dots within the field names.

For the benefit of anyone else who stumbles across this question with keys that do not contain dots, here is search that demonstrates the solution:

| stats count | eval _raw="{ \"key1\":{ \"name\" : \"server1\" }, \"key2\":{ \"name\" : \"server2\" } }" | eval extract_key="key2" | spath | eval desired_name=spath(_raw, extract_key.".name")

(Obviously instead of the stats count and eval _raw you'd be searching an index to get your _raw data.)

The magic is that the spath() eval function can accept a variable for its second argument. By contrast the spath command cannot accept a variable for the path - it treats unquoted paths as literals.

However, changing "key1" and "key2" to IP addresses in the above example breaks it. The problem is that spath uses dots as fieldname separators so when passing an IP address to the spath eval function it looks for a top level object called "10" with a nested object called "10" with a nested object called "1", etc.

To summarise, because dots are separators it's impossible to search for field names containing dots with spath, but it IS possible to "dereference" a field as long as no fields on the path to it contain dots.

dmr195
Communicator

The problem with dots in field names is also covered by this question.

0 Karma

theeansible
Path Finder

Years later and I come across this problem as well thanks.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...