Splunk Search

How do I extract these fields from my data using rex?

IRHM73
Motivator

Hi,

I wonder whether someone could help me please.

I have a field called detail.cid-repsonse which looks like the following:

[{"name":{"current":{"firstName":"JOHN","lastName":"SMITH"}},"ids":{"sut":"1234567890","nino":"AA111111A"},"dateOfBirth":"26121973"}]

From this I need to create new fields and extract the following data:

First Name
Last Name
Sut
NINO
DOB

I just wondered whether someone may be able to offer some guidance on how I may go about this please.

Any help would be greatly appreciated.

Many thanks and kind regards

Chris

0 Karma
1 Solution

tom_frotscher
Builder

Hi,

you simply can do this with the rex command. You can restrict the rex command to one field with the field parameter. Here an example for the first name:

... | rex field="detail.cid-repsonse" "\"firstName\":\"(?<firstName>[^\"]+)" | ...

I created a user everywhere example, which means, you can copy the follwoing search and paste it to your splunk search line and it will work. This can give you an idea how things work:

| stats count | eval "detail.cid-repsonse"="[{\"name\":{\"current\":{\"firstName\":\"JOHN\",\"lastName\":\"SMITH\"}},\"ids\":{\"sut\":\"1234567890\",\"nino\":\"AA111111A\"},\"dateOfBirth\":\"26121973\"}]" | rex field="detail.cid-repsonse" "\"firstName\":\"(?<firstName>[^\"]+)" | rex field="detail.cid-repsonse" "\"lastName\":\"(?<lastName>[^\"]+)" | rex field="detail.cid-repsonse" "\"sut\":\"(?<sut>[^\"]+)" | table "detail.cid-repsonse" firstName lastName sut

By the way, it looks like you have valid json in your field, so you might also be able to use the spath command: http://docs.splunk.com/Documentation/Splunk/6.2.5/SearchReference/Spath

In your case just append this to your search:

| spath input="detail.cid-repsonse"

and you will get new fields with your needed data.

Greetings

Tom

View solution in original post

dmaislin_splunk
Splunk Employee
Splunk Employee

Just use this technique:

http://docs.splunk.com/Documentation/Splunk/6.1.2/Data/Extractfieldsfromfileheadersatindextime

Forwarder props.conf entry for the specific sourcetype.
INDEXED_EXTRACTIONS=JSON

Fields are fed to indexers from the forwarder and searches will be much faster as a result.

0 Karma

tom_frotscher
Builder

I think he does not have json only. Just the field he mentioned in his questions is json. If the complete event is json, your anser might be the even better option.

0 Karma

dmaislin_splunk
Splunk Employee
Splunk Employee

Yes, it speeds up search and offloads the indexers from having to perform line-breaking and timestamp recognition tasks too.

0 Karma

IRHM73
Motivator

Hi @dmaislin, thank you for taking the time to reply to my post. I'm very new to Splunk, so your solution may be a little over my head, but I really appreciate you highlighting something which I will no doubt be able to use in the future.

Many thanks and kind regards

Chris

0 Karma

dmaislin_splunk
Splunk Employee
Splunk Employee

No problem. If the logged events are JSON, this technique is probably the simplest approach as all of your fields will be present without requiring any extra field extraction work.

0 Karma

tom_frotscher
Builder

Hi,

you simply can do this with the rex command. You can restrict the rex command to one field with the field parameter. Here an example for the first name:

... | rex field="detail.cid-repsonse" "\"firstName\":\"(?<firstName>[^\"]+)" | ...

I created a user everywhere example, which means, you can copy the follwoing search and paste it to your splunk search line and it will work. This can give you an idea how things work:

| stats count | eval "detail.cid-repsonse"="[{\"name\":{\"current\":{\"firstName\":\"JOHN\",\"lastName\":\"SMITH\"}},\"ids\":{\"sut\":\"1234567890\",\"nino\":\"AA111111A\"},\"dateOfBirth\":\"26121973\"}]" | rex field="detail.cid-repsonse" "\"firstName\":\"(?<firstName>[^\"]+)" | rex field="detail.cid-repsonse" "\"lastName\":\"(?<lastName>[^\"]+)" | rex field="detail.cid-repsonse" "\"sut\":\"(?<sut>[^\"]+)" | table "detail.cid-repsonse" firstName lastName sut

By the way, it looks like you have valid json in your field, so you might also be able to use the spath command: http://docs.splunk.com/Documentation/Splunk/6.2.5/SearchReference/Spath

In your case just append this to your search:

| spath input="detail.cid-repsonse"

and you will get new fields with your needed data.

Greetings

Tom

IRHM73
Motivator

Hi Tom, this is great and works a treat.

Thank you for taking the time to reply to my post.

Kind Regards and thanks

Chris

0 Karma
Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...