Parsing json where object keys are not quoted?
Intchanter / Daniel Fackrell
unlearned at gmail.com
Thu Dec 10 00:26:16 CET 2009
On Dec 9, 3:51 pm, Wells <thewellsoli... at gmail.com> wrote:
> Is there some way to finagle the json module to parse JSON (well,
> almost JSON) where the object keys are not in quotes? I know it's not
> 100% valid JSON, but I'm just curious.
> I don't have control over the data, so I can't make it fit the spec :)
Hopefully this won't be a recurring problem, because maintenance of
any solution could very well be a nightmare if you have to keep it up.
The JSON library that ships with Python doesn't appear to be built for
malformed JSON like what you mention, and making it handle it will
take a bit of work on your part, but here's a start (based on my 2.6.4
In /path_to_python_standard_library/json/decoder.py (please back this
up before making any changes), comment out the try/except block that
tries to load scanstring from _json and duplicate the last line
(c_scanstring = None), removing its indentation.
You'll then need to modify py_scanstring() to meet your needs, but be
sure you understand what it's doing first. You'll need to track
whether you found the leading '"' for the key and look for the other
one if you did, but just look for the ':' otherwise.
Again, this isn't an advisable solution, and it won't work in all
cases even if you have the best of luck, but it may just work in
enough cases. It's pretty amazing that the incoming document doesn't
match the spec, though. The only correct solution would be to fix the
library that generated it.
More information about the Python-list