简体   繁体   中英

ast.literal_eval throwing ValueError

ast.literal_eval is throwing ValueError: malformed string for the following JSON. I am unable to spot what the error is. I used online JSON validators, all mention that this a valid JSON.

Can someone help me spot the error with this string?

{
    "business_id": "Iu-oeVzv8ZgP18NIB0UMqg",
    "full_address": "3320 S Hill St,South East LA,Los Angeles, CA 90007",
    "schools": [
        "University of Southern California"
    ],
    "open": true,
    "categories": [
        "Medical Centers",
        "Health and Medical"
    ],
    "photo_url": "http://s3-media1.ak.yelpcdn.com/bphoto/SdUWxREuWuPvvot6faxfXg/ms.jpg",
    "city": "Los Angeles",
    "review_count": 2,
    "name": "Southern California Medical Group",
    "neighborhoods": [
        "South East LA"
    ],
    "url": "http://www.yelp.com/biz/southern-california-medical-group-los-angeles",
    "longitude": -118.274281,
    "state": "CA",
    "stars": 3.5,
    "latitude": 34.01971,
    "type": "business"
}

true is an invalid literal in Python:

In [2]: ast.literal_eval('true')
ValueError: malformed node or string: <_ast.Name object at 0x7f32a096d550>

Use json module (specifically json.loads ) when you need to deserialize a JSON document.

You're using the wrong tool for the job here - ast.literal_eval is intended for parsing python literals from strings into python objects. It is not intended for loading data from serialised JSON into python (although it may also work for that in a subset of cases, because serialised json format happens to overlap with python syntax some of the time).

Perhaps you were you looking for json.loads ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM