简体   繁体   中英

Python 2.7 replace all instances of NULL / NONE in complex JSON object

I have the following code..

.... rest api call >> response

rsp = response.json()

print json2html.convert(rsp)

which results in the following

error: Can't convert NULL!

I therefore started looking into schemes to replace all None / Null's in my JSON response, but I'm having an issue since the JSON returned from the api is complex and nested many levels and I don't know where the NULL will actually appear.

From what I can tell I need to iterate over the dictionary objects recursively and check for any values that are NONE and actually rebuild the object with the values replaced, but I don't really know where to start since dictionary objects are immutable..

If you look at json2html's source it seems like you have a different problem - and the error message is not helping.

Try to use it like this:

print json2html.convert(json=rsp)

btw. because I've already contributed to that project a bit I've opened up the following PR due to this question: https://github.com/softvar/json2html/pull/20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM