Splunk Dev

Splunk REST api to retrieve proper JSON using Python?

rajeswarija
New Member

Team

I am able to make a POST call to export end point and getting the search results. I am using Python requests library.
The result is not in proper Json. It returns multiple JSON with preview object. I would like to know how to avoid "preview" to be returned from the response. OR how to overcome the below error? I guess the error is due to the response not in proper Json format.
Appreciate any pointers.

Thanks

Here is the code snippet.

response = requests.post(URL, data=xxxx, headers=headers,verify=False)
print("Status = "+str(response.status_code) )
print("response text = "+str(response.text))
json_data = json.loads(str(r.text))

Sample output

response text = {"preview":true,"offset":0,"lastrow":true,"result":{"_time":"2019-10-09 21:12:00.131 UTC","event_guid":"xxxxxxx","event_category":"WEB","event_workflow":"xxxxx","event_subcategory":"xxxxx"}}
{"preview":true,"offset":0,"lastrow":true,"result":{"_time":"2019-10-09 21:36:06.752 UTC","event_guid":"xxxxxxx-","event_category":"WEB","event_workflow":"xxxxx","event_subcategory":"xxxx"}}
{"preview":false,"offset":0,"result":{"_time":"2019-10-09 21:12:00.131 UTC","event_guid":"xxxx-","event_category":"WEB","event_workflow":"xxxx","event_subcategory":"xxxxx"}}

and so on.. The last command json.loads gives the below error:
I tried without str(). still not working.
Extra data: line 2 column 1 (char 219)

Labels (3)
0 Karma

jim05
Observer

@rajeswarija did you solve this issue? i have the same problem

 

Thanks.

Regards.

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...