Monitoring Splunk

How to do loop on array?

bhanusaketi
Loves-to-Learn

How to loop the array values after split with delimiter 

| eval json="{"key1":"key1value","key2":"key2value","key3":"key3value","key4":"key4Value" }"

| eval keyNames ="key1,key2,key3,key4" // key names can add or remove based on search string the requirement 

 | eval keys=split(keyNames ,";")

How to loop these keys and perform some operation. 

I have tired with some MV commands but no luck.

Example: 

| eval count = mvcount(keys)

| streamstats count as counter

| eval jsonKey= mvindex(keys,count) | eval keyValue = json_extract(json, jsonKey)

I am not sure how to achieve this use case, can some one please help me on it.

 

Labels (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

SPL is not a procedural language so there isn't a "loop through" construct per se. However, it depends on what it is you are trying to achieve as to whether there is another way to do it. For example, if you want to simply extract the key-value pairs from the json string use spath

| eval json="{\"key1\":\"key1value\",\"key2\":\"key2value\",\"key3\":\"key3value\",\"key4\":\"key4Value\" }"
| spath input=json
0 Karma
Get Updates on the Splunk Community!

.conf25 Registration is OPEN!

Ready. Set. Splunk! Your favorite Splunk user event is back and better than ever. Get ready for more technical ...

Detecting Cross-Channel Fraud with Splunk

This article is the final installment in our three-part series exploring fraud detection techniques using ...

Splunk at Cisco Live 2025: Learning, Innovation, and a Little Bit of Mr. Brightside

Pack your bags (and maybe your dancing shoes)—Cisco Live is heading to San Diego, June 8–12, 2025, and Splunk ...