Splunk Search

Is it possible to extract nested data make 2 keys?

batabay
Path Finder

Hi Splunkers,

I have data like this, 

Primary Key_1:

     subkey_1 : subvalue_1

     subkey_2 : subvalue_2

Primary Key_2:

     subkey_1 : subvalue_1

     subkey_2 : subvalue_2

 

I extract data like this, But I want to see in splunk like key1.subkey_1 = sub_value_1

batabay_0-1664448881683.png

 

Btw, this is one event.

I try in transforms.conf ($1::$2:):$3 , But I failed.

And I want to extract this data, What is the best way extract this for as I want. Or is that possible ? 

 

Labels (1)
Tags (3)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

If the data is highly formatted as illustrated and assuming primary key names and values do not contain space, I'd turn the string into conformant data using the following, then you can use spath to extract.

 

| rex mode=sed max_match=0 "s/([^:\s]+)\s*:/\"\1\":/g"
| rex mode=sed max_match=0 "s/: +(\S+)/:\"\1\"/g"
| rex mode=sed "s/^\"/{\"/g"
| rex mode=sed max_match=0 "s/(\s+)\"/\1{\"/g"
| rex mode=sed max_match=0 "s/\"\s\s+{/\", /g"
| rex mode=sed "s/\"\s+{/\"},
 /"
| rex mode=sed "s/\"$/\"}
}/"
| spath

 

PrimaryKey_1.subkey_1PrimaryKey_1.subkey_2PrimaryKey_2.subkey_1PrimaryKey_2.subkey_2
subvalue_1subvalue_2subvalue_1subvalue_2

In case you have this question in mind: This cannot be implemented in transforms.conf.

View solution in original post

0 Karma

yuanliu
SplunkTrust
SplunkTrust

If the data is highly formatted as illustrated and assuming primary key names and values do not contain space, I'd turn the string into conformant data using the following, then you can use spath to extract.

 

| rex mode=sed max_match=0 "s/([^:\s]+)\s*:/\"\1\":/g"
| rex mode=sed max_match=0 "s/: +(\S+)/:\"\1\"/g"
| rex mode=sed "s/^\"/{\"/g"
| rex mode=sed max_match=0 "s/(\s+)\"/\1{\"/g"
| rex mode=sed max_match=0 "s/\"\s\s+{/\", /g"
| rex mode=sed "s/\"\s+{/\"},
 /"
| rex mode=sed "s/\"$/\"}
}/"
| spath

 

PrimaryKey_1.subkey_1PrimaryKey_1.subkey_2PrimaryKey_2.subkey_1PrimaryKey_2.subkey_2
subvalue_1subvalue_2subvalue_1subvalue_2

In case you have this question in mind: This cannot be implemented in transforms.conf.

0 Karma

batabay
Path Finder

Some of pieces of data different but I'll try to change. The answer is correct.

0 Karma

jdunlea
Contributor

Presumably this is a multi line event?

Are there finite limits to how many primary keys will be in the data? Like are there always 2 primary keys? Or could there be more/less depending on the event that comes in?

0 Karma
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...