Splunk Search

Extract distinct values from an response array that has partial text using RegEx

balamurugandha7
Observer

I have an array that would be presented in an API response which is being logged in Splunk and the array format is like this:

"Indicators": [
                "HAS_ACOUNT",
                "NON_ZERO_BALANCE_ACCOUNT",
                "JOINT_ACCOUNT",
                "NOT_EXPIRED",
                "REGISTERED"
            ]
 
The number of values within the array will not always have same pattern. i.e. There may be responses where there may be 10 values within 'Indicators' array.
 
Now, I want to extract distinct values within "Indicators" array (with the value that has the text "ACCOUNT") logged in Splunk for last 30 days from that specific API response. Could someone help me how to get that?
 
I wrote like this, but is didn't quite capture all possible values..

index="index_name" Env=test "........./API" | rex field=_raw "\"Indicators\"\:\[(?<planInd>[^\,]*)\]" max_match=0 | where like (planInd,"%PCP%") | dedup planInd | table planInd
Labels (3)
0 Karma

balamurugandha7
Observer

When I ran this I got the same array that is assigned to "_raw". 

What I was expecting as an output is,
"HAS_ACOUNT",
"NON_ZERO_BALANCE_ACCOUNT",
"JOINT_ACCOUNT"

as only these three values within the Indicators array has the partial text "ACCOUNT". Also I couldn't figure out in your query where exactly you are trying to extract the events that has the partial text "ACCOUNT".

For example, if there is another event that has the "Indicators" array value as follows, then I should only get the value "HAS_ACCOUNT" for this event as this is the only value within that array that has the partial text "ACCOUNT"..

"Indicators": [
          "HAS_ACOUNT",
          "NOT_EXPIRED",
]

0 Karma

to4kawa
Ultra Champion
index=_internal | head 1 | fields _raw
| eval _raw="\"Indicators\": [
                \"HAS_ACCOUNT\",
                \"NON_ZERO_BALANCE_ACCOUNT\",
                \"JOINT_ACCOUNT\",
                \"NOT_EXPIRED\",
                \"REGISTERED\"
            ]"
| rex mode=sed "s/(?ms)(.*)/{\1}/"
| spath
| rename *{} as *
| rex field=Indicators max_match=0 "(?<indicators>.*ACCOUNT)"
| table indicators
0 Karma

balamurugandha7
Observer

Please advise how am I suppose to dynamically pass values to "elav _raw...." as I am trying to apply this rex to all events logged for last 30 days.

Thanks in advance!

0 Karma

to4kawa
Ultra Champion

The rex is performed for each event, so I am not sure what the problem is.

And it doesn't say how the logs for the past 30 days will appear.

0 Karma

to4kawa
Ultra Champion
index=_internal | head 1 | fields _raw
| eval _raw="\"Indicators\": [
                \"HAS_ACOUNT\",
                \"NON_ZERO_BALANCE_ACCOUNT\",
                \"JOINT_ACCOUNT\",
                \"NOT_EXPIRED\",
                \"REGISTERED\"
            ]"
| rex mode=sed "s/(?ms)(.*)/{\1}/"
| spath

I think you will make it JSON better.

0 Karma
Get Updates on the Splunk Community!

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...

Purpose in Action: How Splunk Is Helping Power an Inclusive Future for All

At Cisco, purpose isn’t a tagline—it’s a commitment. Cisco’s FY25 Purpose Report outlines how the company is ...

[Upcoming Webinar] Demo Day: Transforming IT Operations with Splunk

Join us for a live Demo Day at the Cisco Store on January 21st 10:00am - 11:00am PST In the fast-paced world ...