How to Deal with Malformed JSON

Discuss Python scripts here.
Bollar
Posts: 528
Joined: Sun Aug 11, 2013 3:14 pm

How to Deal with Malformed JSON

Post by Bollar »

The JSON returned by the Roomba I'm trying to communicate with is malformed and causes a ValueError exception when using requests.json() or json.loads(). Does anyone have an idea on how to fix/filter/resolve the problem?

Here's a sample string:

Code: Select all

{"ok":{"d":"sun","h":19,"m":22},"id":1"}}
and another:

Code: Select all

{"ok":{"flags":0,"cycle":"none","phase":"charge","pos":{"theta":-90,"point":{"x":-495,"y":792}},"batPct":96,"expireM":0,"rechrgM":0,"error":0,"notReady":0,"mssnM":514,"sqft":999},"id":1"}}
Regardless, they all fail with this error:

Code: Select all

 Error: Parse error on line 21:
...": 999	},	"id": 1 "}}
---------------------^
Expecting 'EOF', '}', ',', ']', got 'undefined'
Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: How to Deal with Malformed JSON

Post by jay (support) »

There appear to be 2 problems: the extra quote after the 1 at the end - it doesn't match any other quote, and the extra closing brace at the end. You can attempt to fix those very specific issues, but it seems likely there will be other issues as well. I would contact Roomba and complain that their API is just broken...
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
Bollar
Posts: 528
Joined: Sun Aug 11, 2013 3:14 pm

Re: How to Deal with Malformed JSON

Post by Bollar »

jay (support) wrote:...their API is just broken...
Their API is just unpublished... Hence the workarounds. Any advice?
Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: How to Deal with Malformed JSON

Post by matt (support) »

If the problem is always that on the end there is:

"}}

But there should be just:

}

Then you could try parsing the JSON, then if there is the exception you could hack the raw JSON string to force the "}} to be a }, then re-run the parser on it?
Image
Bollar
Posts: 528
Joined: Sun Aug 11, 2013 3:14 pm

Re: How to Deal with Malformed JSON

Post by Bollar »

Ah.

Code: Select all

r = requests.post(post_url, json=payload, headers=head, verify=False)

broken = str(r.text)
fixed = broken.replace('"''}','')
Results:

Code: Select all

   Script                          {"ok":{"d":"sun","h":21,"m":45},"id":1"}}
   Script                          {"ok":{"d":"sun","h":21,"m":45},"id":1}
Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts
Post Reply

Return to “Python Scripting”