Python Forum
how do I format json data in splunk? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how do I format json data in splunk? (/thread-3197.html)

Pages: 1 2


how do I format json data in splunk? - fxtyom - May-04-2017

I wrote a python script in splunk to make API calls and display output to a menu page for my dashboard. I'm not able to get it to display json output. How can I get it to display output formatted correctly. this is the response back from API call --


response = requests.post(url, data=json.dumps(payload))     

print('printing response ', response.json())



RE: how do I format json data in splunk? - volcano63 - May-04-2017

You may try pprint package


RE: how do I format json data in splunk? - snippsat - May-04-2017

(May-04-2017, 06:55 PM)volcano63 Wrote: I wrote a python script in splunk to make API calls and display output to a menu page for my dashboard
Is this dashboard a website?
Do you want to display the raw JSON,or the more normal approach parse JSON and inserts value into HTML/CSS so it look better?


RE: how do I format json data in splunk? - volcano63 - May-05-2017

(May-04-2017, 09:15 PM)snippsat Wrote:
(May-04-2017, 06:55 PM)volcano63 Wrote: I wrote a python script in splunk to make API calls and display output to a menu page for my dashboard
Is this dashboard a website?
Do you want to display the raw JSON,or the more normal approach parse JSON and inserts value into HTML/CSS so it look better?
Wrong quote!
Quoting @wavic -
Quote:too much vodka
Wink ?!


RE: how do I format json data in splunk? - fxtyom - May-08-2017

I'm not able to print python API response in splunk. Can I use ResultsReader to display my response?


RE: how do I format json data in splunk? - nilamo - May-08-2017

Looks like splunk has an api: http://dev.splunk.com/python
You should use that to shove data into it.  ...unless they read from stdout (is that why you used print?)


RE: how do I format json data in splunk? - fxtyom - May-09-2017

I would like to parse JSON and insert values into HTML/CSS splunk dashboard, so it looks better. i'm using print statements, because my knowledge of python is limited. My dashboard is in HTML/CSS. How can I display what is coming back in my JSON object to a splunk HTML dashboard? I can display output when I run script in my Spyder SDK using simple print commands. But this does not work in splunk, nothing gets printed out on screen.


RE: how do I format json data in splunk? - fxtyom - May-24-2017

I've made some progress, changed the way I make API call., but still having issues with displaying data in splunk. this is part of my code and error message i'm getting. Not sure how to resolve issue?
import requests
import csv
import json
import sys
try: from splunklib.results import *
except: print(sys.exc_info())
try: import httplib
except: print(sys.exc_info())
try: import urllib
except: print(sys.exc_info())

payload = {
     "application": [
       "app_name",
       "description",
       "top_app",
       "status",
       "app_type",
       "gaca_ref",
       "created_on",
       "modified_on",
       "alias_name",
       "business_segment",
       "business_solution",
       "lob",
       "schd_maintenance",
       "schd_outage",
       "vendor_contract",
       "vendor_name",
       "platforms",
       "products",
       "clients",
       "countries"
     ],
     "fields": "custom",
     "uaid_list": ['']}

def getAppMapApplication(uaid_num, user_name, pass_word):
   payload['uaid_list'] = [uaid_num]
   data = loadInputCredentials(user_name, pass_word)
   token = getTokenId(data)
   url2 = "/api/v3/appmap/applications/?format=json&username=abbr3f5&api_token=" + token
   conn = httplib.HTTPSConnection("echo.1dc.com")
   try: conn.request("POST", url2, json.dumps(payload))
   except: print(sys.exc_info())
   try: response = conn.getresponse()
   except: print(sys.exc_info())

   temp = response.read()
   conn.close()
   try: reader = results.ResultsReader(response)
   except: print(sys.exc_info())
   for result in reader:
       print("print result", result.keys())
       if isinstance(result, dict):
           print("Result: %s" % result)
       elif isinstance(result, Message):
           print("Message: %s" % result)
   print("is_preview = %s " % reader.is_preview)


   return response



error message i'm getting:
(<type 'exceptions.NameError'>, NameError("global name 'results' is not defined",), <traceback object at 0x0000000002DB9908>)
Traceback (most recent call last):
 File "C:/Users/tajones/PycharmProjects/appMapAPI/API_calls.py", line 296, in <module>
   main(sys.argv)
 File "C:/Users/tajones/PycharmProjects/appMapAPI/API_calls.py", line 291, in main
   getAppMapApplication(args[2],args[3],args[4])
 File "C:/Users/tajones/PycharmProjects/appMapAPI/API_calls.py", line 195, in getAppMapApplication
   for result in reader:
UnboundLocalError: local variable 'reader' referenced before assignment

Process finished with exit code 1


RE: how do I format json data in splunk? - fxtyom - May-24-2017

I hope this if better>
My API call is working, getting response back. i'm not able to display response using ResultsReader? getting error - exceptions.NameError'>, NameError("global name 'results' is not defined

def getAppMapApplication(uaid_num, user_name, pass_word):
   payload['uaid_list'] = [uaid_num]
   data = loadInputCredentials(user_name, pass_word)
   token = getTokenId(data)
   url2 = "URI string" + token
   conn = httplib.HTTPSConnection("URL server name")
   try: conn.request("POST", url2, json.dumps(payload))
   except: print(sys.exc_info())
   try: response = conn.getresponse()
   except: print(sys.exc_info())

   temp = response.read()
   conn.close()
   try: reader = results.ResultsReader(response)
   except: print(sys.exc_info())
   for result in reader:
       print("print result", result.keys())
       if isinstance(result, dict):
           print("Result: %s" % result)
       elif isinstance(result, Message):
           print("Message: %s" % result)
   print("is_preview = %s " % reader.is_preview)

   return response

error message i'm getting:
(<type 'exceptions.NameError'>, NameError("global name 'results' is not defined",), <traceback object at 0x0000000002DB9908>)
Traceback (most recent call last):
   for result in reader:
UnboundLocalError: local variable 'reader' referenced before assignment

Process finished with exit code 1


RE: how do I format json data in splunk? - micseydel - May-24-2017

Replace
try: reader = results.ResultsReader(response)
except: print(sys.exc_info())
with
reader = results.ResultsReader(response)
The exception handling doesn't make sense, the program needs to exit if any exception occurs because the variable won't be defined (as the error message says). If you let the exception be thrown, you should get some information that will help with identifying the root issue.