Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parsing json where object keys are not quoted?

Reply
Thread Tools

Parsing json where object keys are not quoted?

 
 
Wells
Guest
Posts: n/a
 
      12-09-2009
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
 
Reply With Quote
 
 
 
 
Intchanter / Daniel Fackrell
Guest
Posts: n/a
 
      12-09-2009
On Dec 9, 3:51*pm, Wells <(E-Mail Removed)> 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
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
JSON object not parsing in firefox saril.ks@gmail.com Javascript 11 09-30-2007 09:02 AM



Advertisments