Splunk Search

How to extract values inside of the {}?

napoleon182
Explorer

Hello Splunk Ninjas!

I will require your assistance with designing my regex expression.

I need to filter for the value of Message in this sample log line:

 

2022-09-23T13:20:25.765+01:00 [29] WARN Core.ErrorResponse - {} - Error message being sent to user with Http Status code: BadRequest:
{"Details":{"field1":value,"field2":"value2"},"Message":"This is the message.","UserMessage":null,"Code":86,"Explanation":null,"Resolution":null,"Category":4}

 

I will be interested in extracting value of field1, field2 (inside of {Details}, message and code,

Any help, much appreciated!

Thanks again

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

This is fairly simple, but I suspect you are struggling to extract all fields with a single regex.  Don't worry about that.  Using multiple rex commands is much easier and has little performance penalty.  A complex regex that extracts all fields in one go may be less performant.

| rex "Message\\\":\\\"(?<Message>[^\\\"]+)"
| rex "field1\\\":\\\"(?<field1>[^\\\"]+)"
| rex "field2\\\":\\\"(?<field2>[^\\\"]+)"
| rex "Code\\\":(?<Code>\d+)"
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

This is fairly simple, but I suspect you are struggling to extract all fields with a single regex.  Don't worry about that.  Using multiple rex commands is much easier and has little performance penalty.  A complex regex that extracts all fields in one go may be less performant.

| rex "Message\\\":\\\"(?<Message>[^\\\"]+)"
| rex "field1\\\":\\\"(?<field1>[^\\\"]+)"
| rex "field2\\\":\\\"(?<field2>[^\\\"]+)"
| rex "Code\\\":(?<Code>\d+)"
---
If this reply helps you, Karma would be appreciated.

gcusello
SplunkTrust
SplunkTrust

@napoleon182,

this seems to be a json format, did you tried with spath command (https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath)?

Ciao.

Giuseppe

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...