Splunk Enterprise

How to map extracted keys to values and separate them?

piyushpandey
Engager

I have logs which contain parts like:
.. { "profilesCount" : { "120000" : 100 , "120001" : 500 , "110105" : 200 , "totalProfilesCount" : 1057}} ..
here the key is accountId and value is the number of profiles in it.

when I use max_count=0 in rex and extract these values I get:
accountId=[12000000, 12000001, 11001005] and pCount=[100, 500, 200] for this example event.

Since these accountIds are not mapped to their corresponding pCount when I visualize them I get

accountId pCount
12000000 100
500
200
12000001 100
500
200
11001005 100
500
200


how can I map them correctly and show in a table form?

This was my search query:
search <search_logic> | rex max_match=0 "\"(?<account>\d{8})\" : (?<pCount>\d+)"] | stats values(pCount) by account

Thanks in advance

Labels (1)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

If your data is JSON, then spath or json_extract can extract that, e.g. something like

| eval p=json_extract(_raw, "profilesCount")
| spath input=p
| fields - _raw _time totalProfilesCount p
| transpose

or you can use foreach to make a field of the account, e.g. 

| rex max_match=0 "\"(?<account>\d{8})\" : (?<pCount>\d+)"
| foreach 0 1 2 3 4 5 [ eval n=mvindex(account,<<FIELD>>), {n}=mvindex(pCount,<<FIELD>>) | fields - n]

or you can use zip/expand/extract

| eval zip=mvzip(account, pCount, "=")
| fields - account pCount
| mvexpand zip
| rex field=zip "(?<account>\d{8})=(?<pCount>\d+)"

Hopefully one of these approaches will get you where you want to get to

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

If your data is JSON, then spath or json_extract can extract that, e.g. something like

| eval p=json_extract(_raw, "profilesCount")
| spath input=p
| fields - _raw _time totalProfilesCount p
| transpose

or you can use foreach to make a field of the account, e.g. 

| rex max_match=0 "\"(?<account>\d{8})\" : (?<pCount>\d+)"
| foreach 0 1 2 3 4 5 [ eval n=mvindex(account,<<FIELD>>), {n}=mvindex(pCount,<<FIELD>>) | fields - n]

or you can use zip/expand/extract

| eval zip=mvzip(account, pCount, "=")
| fields - account pCount
| mvexpand zip
| rex field=zip "(?<account>\d{8})=(?<pCount>\d+)"

Hopefully one of these approaches will get you where you want to get to

Get Updates on the Splunk Community!

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

🔐 Trust at Every Hop: How mTLS in Splunk Enterprise 10.0 Makes Security Simpler

From Idea to Implementation: Why Splunk Built mTLS into Splunk Enterprise 10.0  mTLS wasn’t just a checkbox ...