Getting Data In

How to search JSON data

vbotnari1
Engager

I am trying to parse this json using spath

 {  
       "Request":{  
          "Uri":"/api/...",
          "requestHeaders":[  

             {  
                "Key":"X-Real-IP",
                "Value":[  
                   "213.205.240.26, 55.145.164.26"
                ]
             },
             {  
                "Key":"X-Forwarded-For",
                "Value":[  
                   "111.111.240.26"
                ]
             },

          "ContentHeaders":[  
             {  
                "Key":"Content-Length",
                "Value":[  
                   "0"
                ]
             }
          ],
          "Body":null
       },
       "Response":{  },
       "TimeTakenMs":119
    }

I need to extract the first value for X-Real-IP. Any help is appreciated.

Tags (2)
0 Karma
1 Solution

FrankVl
Ultra Champion

Looking at your data again: the difficulty is that X-Real-IP is not an actual json key that you can target with spath. It is the value of a field called Key.

You can extract this, but not with just using spath.

In this particular case a simple regex might be easier:

...
| rex "\"Key\":\"X-Real-IP\",\s*\"Value\":\[\s*\"(?<X_Real_IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"

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

View solution in original post

0 Karma

FrankVl
Ultra Champion

Looking at your data again: the difficulty is that X-Real-IP is not an actual json key that you can target with spath. It is the value of a field called Key.

You can extract this, but not with just using spath.

In this particular case a simple regex might be easier:

...
| rex "\"Key\":\"X-Real-IP\",\s*\"Value\":\[\s*\"(?<X_Real_IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"

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

0 Karma

FrankVl
Ultra Champion

What do you have so far and what exactly do you need help with?

0 Karma

vbotnari1
Engager

I tried to create a span path to the correct Key (X-Real-IP) but it didn't work, my experience with json paths is not great

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...

Customer success is front and center at .conf25

Hi Splunkers, If you are not able to be at .conf25 in person, you can still learn about all the latest news ...

.conf25 Global Broadcast: Don’t Miss a Moment

Hello Splunkers, .conf25 is only a click away.  Not able to make it to .conf25 in person? No worries, you can ...