JSS API how to get JSON instead of XML with Python

steven_luke
New Contributor

Hello,
I'm trying to collect inventory data from the JSS via API calls using Python.

When I make a request

import requests 
jss_application_usage = requests.get('https://domain.jamfcloud.com/JSSResource/computerapplicationusage/id/%s/%s_%s' % (computer_id, yesterday, today), auth=HTTPBasicAuth(api_user, api_password))

and try to see what type of content gets sent to me,

print jss_application_usage.headers['content-type']

I only get text/xml;charset=UTF-8 as my response.

So my question is, is there a way to get back json data instead of XML? Is this something that needs to be configured on the JSS?

Thanks!

1 ACCEPTED SOLUTION

chriscollins
Valued Contributor

@steven.luke You need to specify the headers in your requests get.

jss_application_usage = requests.get('https://domain.jamfcloud.com/JSSResource/computerapplicationusage/id/%s/%s_%s' % (computer_id, yesterday, today), auth=HTTPBasicAuth(api_user, api_password), headers={'Accept': 'application/json'})

There is a convenient method in requests to convert that data to a dictionary without you having to use the json module

import requests
jss_application_usage = requests.get('https://domain.jamfcloud.com/JSSResource/computerapplicationusage/id/%s/%s_%s' % (computer_id, yesterday, today), auth=HTTPBasicAuth(api_user, api_password), headers={'Accept': 'application/json'})

jss_json = jss_application_usage.json()

Just remember if you don't already know that you can only retrieve json data, you can't submit it back (only accepts xml). The new Universal API will use json for getting and posting in the future.

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

I don't know Python, so I'm not the best person to give an answer, but in bash, when using curl to access the API, I add a header flag to tell the API what format I want the data in. Ironically, I need to do this because I prefer xml over json, and usually get JSON when doing a GET, unless I specify I want text/xml. So kind of the opposite of what you're seeing.

Is there an equivalent header flag you can use with Python in your script to specify you want the response in JSON format?

chriscollins
Valued Contributor

@steven.luke You need to specify the headers in your requests get.

jss_application_usage = requests.get('https://domain.jamfcloud.com/JSSResource/computerapplicationusage/id/%s/%s_%s' % (computer_id, yesterday, today), auth=HTTPBasicAuth(api_user, api_password), headers={'Accept': 'application/json'})

There is a convenient method in requests to convert that data to a dictionary without you having to use the json module

import requests
jss_application_usage = requests.get('https://domain.jamfcloud.com/JSSResource/computerapplicationusage/id/%s/%s_%s' % (computer_id, yesterday, today), auth=HTTPBasicAuth(api_user, api_password), headers={'Accept': 'application/json'})

jss_json = jss_application_usage.json()

Just remember if you don't already know that you can only retrieve json data, you can't submit it back (only accepts xml). The new Universal API will use json for getting and posting in the future.