Search code examples
pythonjsonsimplejson

Transform invalid nested json to valid one and change to list


Under this link :
http://dev1.gecoloco.com/rte/done_json.php I have a json-like object, that I'm operating on. I cannot load it with simplejson, because it is wrongly formatted. And thus this code fails :

conn = httplib.HTTPConnection("dev1.gecoloco.com")
conn.request("GET", "/rte/done_json.php")
r = conn.getresponse()
data = r.read()
logging.debug(data)
json = simplejson.loads(data)

As a result I'd like to get a list of dictionaries.
So first question is how to load it as proper json? Read as string and then replace quotes or something different ?
Second question is how to transform the properly formatted json to a list of dictionaries ? (Do I even need json to do this easily?) ?
Thanks for any help.


Solution

  • The nice thing about JSON is that it's so, so very close to Python literal syntax. Use ast.literal_eval() to parse it.