frazman
frazman

Reputation: 33223

unable to parse string to json in python

I have a string, which I evaluate as:

import ast
def parse(s):
    return ast.literal_eval(s)   


print parse(string)

 {'_meta': {'name': 'foo', 'version': 0.2},
     'clientId': 'google.com',
     'clip': False,
     'cts': 1444088114,
     'dev': 0,
     'uuid': '4375d784-809f-4243-886b-5dd2e6d2c3b7'}

But when I use jsonlint.com to validate the above json.. it throws schema error..

If I try to use json.loads I see the following error:

Try: json.loads(str(parse(string)))
    ValueError: Expecting property name: line 1 column 1 (char 1)

I am basically trying to convert this json in avro How to covert json string to avro in python?

Upvotes: 0

Views: 2207

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121644

ast.literal_eval() loads Python syntax. It won't parse JSON, that's what the json.loads() function is for.

Converting a Python object to a string with str() is still Python syntax, not JSON syntax, that is what json.dumps() is for.

JSON is not Python syntax. Python uses None where JSON uses null; Python uses True and False for booleans, JSON uses true and false. JSON strings always use " double quotes, Python uses either single or double, depending on the contents. When using Python 2, strings contain bytes unless you use unicode objects (recognisable by the u prefix on their literal notation), but JSON strings are fully Unicode aware. Python will use \xhh for Unicode characters in the Latin-1 range outside ASCII and \Uhhhhhhhh for non-BMP unicode points, but JSON only ever uses \uhhhh codes. JSON integers should generally be viewed as limited to the range representable by the C double type (since JavaScript numbers are always floating point numbers), Python integers have no limits other than what fits in your memory.

As such, JSON and Python syntax are not interchangeable. You cannot use str() on a Python object and expect to parse it as JSON. You cannot use json.dumps() and parse it with ast.literal_eval(). Don't confuse the two.

Upvotes: 3

Related Questions