Splunk Dev

How do I extract a field from the _raw data

danielsan
New Member

Hi guys,

Tried my best googling an answer but have not been able to find something that fits my requirement.

I have the following raw_data:

... \"number_of_goods\":10,\"number_of_locations\":5, ...

How do I create a field called number_of_goods and number_of_locations where the value is 10 and 5?

Many thanks in advance!

Tags (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Try the rex command. The backslashes are needed to escape the backslashes and quotes in your data.

<your search> | rex "number_of_goods\\\\\":(?<numberOfGoods>\d+),\\\\\"number_of_locations\\\\\":(?<numberOfLocations>\d+)" | ...
---
If this reply helps you, Karma would be appreciated.

danielsan
New Member

It works, you really saved my day. I have one follow-up question though. I'm trying to extract a similar field but it has a decimal value, e.g. 0.25 and with the rex example I got from you, I only get the first digit before the decimal and I can't figure out how to get the full value.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The regex for a floating point number is \d+\.\d+.

---
If this reply helps you, Karma would be appreciated.
0 Karma

richgalloway
SplunkTrust
SplunkTrust

If your problem is resolved, please accept the answer to help future readers.

---
If this reply helps you, Karma would be appreciated.
0 Karma

FrankVl
Ultra Champion

I guess you could also avoid having to mess with those backslashes and escaped quotes, by doing:

| rex "number_of_goods[^:]+:(?<numberOfGoods>\d+),[^\w]+number_of_locations[^:]+:(?<numberOfLocations>\d+)"

https://regex101.com/r/RgxoTh/1

Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...