Parsing json where object keys are not quoted?

Discussion in 'Python' started by Wells, Dec 9, 2009.

  1. Wells

    Wells Guest

    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 :)
    Wells, Dec 9, 2009
    #1
    1. Advertising

  2. On Dec 9, 3:51 pm, Wells <> 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.
    Intchanter / Daniel Fackrell, Dec 9, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    241
    Florian Frank
    Jun 30, 2009
  2. Replies:
    11
    Views:
    277
    Thomas 'PointedEars' Lahn
    Sep 30, 2007
  3. sajuptpm
    Replies:
    2
    Views:
    338
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    254
    dieter
    Feb 15, 2013
  5. David Karr
    Replies:
    1
    Views:
    166
    David Karr
    Jun 17, 2013
Loading...

Share This Page