Getting this back from a JSON object:
The call is made here:
response = make_request(GET_QUALIFIED_OFFERS_URL, request)
def make_request(url, json_data):
host = url
req = urllib2.Request(host, json_data, {'content-type': 'application/json'})
response_stream = urllib2.urlopen(req)
return response_stream.read()
response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":true}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4}
print json.dumps((response), sort_keys=True, indent=4)
Getting an error:
print json.dumps({"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":true}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4)
NameError: global name 'true' is not defined
It looks like some of the JSON isn't correct. I put quotes around the value "true" and it works. So is there any way to put quotes around all the values?
This Works:
response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":"true"}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4}
The problem is I have JSON like this all over the place with values like false and true with no quotes in huge key--value data sets.
What I am trying to do is take the JSon and make it pretty to be able to compare against it. I am trying to write an automation frame work to test what comes back in the Json. Ideally I would love to create like a csv ouput. Maybe have a column for each key and then have a row for each value. Anyone else doing something like this?
There's difference between writing pure JSON-string and converting a Python data structure into JSON-string.
Python's
JSON{En,De}coder
performs following translations by default:So when you write
{'foo': True}
in Python, JSONEncoder writes out{'foo': true}
as per JSON-standard.In Python the keyword is
True
, nottrue
. Case sensitivity counts. By way of example usage for bothdumps
andloads
: