5

I have a python script that I want to print the JSON output to look something like this:

{
    "authMode": "open",
    "enabled": false,
    "ipAssignmentMode": "NAT mode",
    "name": "Unconfigured SSID 14",
    "number": 13,
    "perClientBandwidthLimitDown": 0,
    "perClientBandwidthLimitUp": 0,
    "splashPage": "None",
    "ssidAdminAccessible": false
},
{
    "authMode": "open",
    "enabled": false,
    "ipAssignmentMode": "NAT mode",
    "name": "Unconfigured SSID 15",
    "number": 14,
    "perClientBandwidthLimitDown": 0,
    "perClientBandwidthLimitUp": 0,
    "splashPage": "None",
    "ssidAdminAccessible": false
}

But my output looks like this instead:

{u'authMode': u'open',
u'enabled': False,
u'ipAssignmentMode': u'NAT mode',
u'name': u'Unconfigured SSID 14',
u'number': 13,
u'perClientBandwidthLimitDown': 0,
u'perClientBandwidthLimitUp': 0,
u'splashPage': u'None',
u'ssidAdminAccessible': False},
{u'authMode': u'open',
u'enabled': False,
u'ipAssignmentMode': u'NAT mode',
u'name': u'Unconfigured SSID 15',
u'number': 14,
u'perClientBandwidthLimitDown': 0,
u'perClientBandwidthLimitUp': 0,
u'splashPage': u'None',
u'ssidAdminAccessible': False}]

I feel like there is something simple I am missing. Here is my code:

url = "https://dashboard.meraki.com/api/v0/networks/%s/ssids" % NETWORKID
headers = {'X-Cisco-Meraki-API-Key': APIKEY}
r = requests.get(url, headers=headers, allow_redirects=True)
pprint (r.json())
justin
  • 61
  • 1
  • 1
  • 2
  • You're using `pprint`, a module that basically serializes data back into native python syntax. using regular `print` will output the value as a string, if possible. If you want to get the json data as a string I think you want `r.body`, I think `r.json` is the decoded value, eg, its already been turned into native python types for you. – ThorSummoner Dec 15 '16 at 21:26

1 Answers1

14

I've found the easiest way to accomplish this is to use the json module:

r = requests.get(url, headers=headers, allow_redirects=True)
import json
print(json.dumps(r.json(), indent=2))

Yes, this is a bit redundant in that you are deserializing then serializing again, but it works.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172