How do I check if a string is valid JSON in Python?

后端 未结 4 1084
南旧
南旧 2020-12-07 10:03

In Python, is there a way to check if a string is valid JSON before trying to parse it?

For example working with things like the Facebook Graph API, sometimes it r

4条回答
  •  余生分开走
    2020-12-07 10:39

    You can try to do json.loads(), which will throw a ValueError if the string you pass can't be decoded as JSON.

    In general, the "Pythonic" philosophy for this kind of situation is called EAFP, for Easier to Ask for Forgiveness than Permission.

提交回复
热议问题