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.

2 Answers 2

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'}
Sign up to request clarification or add additional context in comments.

1 Comment

Right. There is also the original, unsafe, built-in eval() as well. But don't use eval(), use ast.literal_eval(). stackoverflow.com/questions/15197673/…
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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.