Parsing json where object keys are not quoted?

W

Wells

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 :)
 
I

Intchanter / Daniel Fackrell

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
installation):

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top