Alerting

How to write regular expression for the below log and to extract Error code, ErrorMessage ?

venkatanagendra
New Member

Activity Result: {"IsProductValidated":"false","ErrorCodes":[{"errorCode":"PRD-202","errorMessage":"Product Validation Service Returned Error :: Reason: Options you have selected are not available at this time. Please change your selections."}]}

Labels (2)
0 Karma

pl2345
Path Finder

You could use field extraction or the rex command.

| rex field="Activity Result" "Activity Result: {"IsProductValidated":"false","ErrorCodes":\[{"errorCode":"(?P<errorCode>\S+)","errorMessage":"(?P<errorMessage>.+)."}]}" 

0 Karma

schose
Builder

Hi @venkatanagendra ,

You do not need to do regex, as your data is already in  structured json format. Ensure your sourcetype is using kv_mode=json  or kv_mode=auto and you can address fields 

ErrorCodes{}.errorCode ErrorCodes{}.errorMessage

Best regards,

Andreas

 

0 Karma

venkatanagendra
New Member

Thanks for your quick response schose...but  if you see below image mentioned log comeing from (Alexinfomsg ) field, in this field Activity Result have Error code and Error message, these information we need to extract

venkatanagendra_0-1667322669431.png

 

0 Karma

schose
Builder

Hi @venkatanagendra ,

If the json you provided is part of an already existing field i would go with eval json functions

 

| makeresults
| eval json = "{\"IsProductValidated\":\"false\",\"ErrorCodes\":[{\"errorCode\":\"PRD-202\",\"errorMessage\":\"Product Validation Service Returned Error :: Reason: Options you have selected are not available at this time. Please change your selections.\"}]}"
| eval errorCode = json_extract(json,"ErrorCodes{}.errorCode"), errorMessage = json_extract(json,"ErrorCodes{}.errorMessage")
| table _time errorCode errorMessage

 

Best Regards,

Andreas

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...