2

So, I had someone send me a JSON dump of some data, but they obviously did it lazily (by printing) in python so that the (simplified) data is:

{u'x': u'somevalue', u'y': u'someothervalue'}

instead of valid JSON:

{"x": "somevalue", "y": "someothervalue"}

Since it's not valid JSON, json.loads() naturally fails to parse it.

Does Python include any modules to parse its own output like this? I actually think parsing it myself might be faster than trying to explain to this guy what he did wrong and how to fix it.

Josh Buell
  • 606
  • 5
  • 12

2 Answers2

4

You might be able to get away with the following:

>>> s = "{u'x': u'somevalue', u'y': u'someothervalue'}"
>>> from ast import literal_eval
>>> literal_eval(s)
{u'y': u'someothervalue', u'x': u'somevalue'}
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 1
    Right. There is also the original, unsafe, built-in `eval()` as well. But don't use `eval()`, use `ast.literal_eval()`. http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – steveha Jun 03 '13 at 22:19
1

The demjson python module allows for strict and non-strict operation. Here's a list of some of the allowances in non-strict mode:

The following are permitted when processing in NON-STRICT mode:

* Unicode format control characters are allowed anywhere in the input.
* All Unicode line terminator characters are recognized.
* All Unicode white space characters are recognized.
* The 'undefined' keyword is recognized.
* Hexadecimal number literals are recognized (e.g., 0xA6, 0177).
* String literals may use either single or double quote marks.
* Strings may contain \x (hexadecimal) escape sequences, as well as the
  \v and \0 escape sequences.
* Lists may have omitted (elided) elements, e.g., [,,,,,], with
  missing elements interpreted as 'undefined' values.
* Object properties (dictionary keys) can be of any of the
  types: string literals, numbers, or identifiers (the later of
  which are treated as if they are string literals)---as permitted
  by ECMAScript.  JSON only permits strings literals as keys.
Mike Pelley
  • 2,939
  • 22
  • 23
  • This was going to be an additional suggestion to my answer, but I couldn't get it to work - any idea if there's any argument that should be used to meet the OP's requirement? – Jon Clements Jun 03 '13 at 22:19
  • @Jon Clements When you initialize the JSON class you can pass in `strict=False` which enables the list of allowances I posted above. From reading Josh's example, it looks like they should allow the content to be parsed. However, for the specific use-case listed by Josh, I think your answer is a much better solution anyway :) – Mike Pelley Jun 03 '13 at 22:21