Verify if a String is JSON in python?

后端 未结 4 580
遇见更好的自我
遇见更好的自我 2020-12-29 05:12

I have a string in Python, I want to know if it is valid JSON.

json.loads(mystring) will raise an error if the string is not JSON but I don\'t want to

4条回答
  •  孤独总比滥情好
    2020-12-29 05:49

    why parsing when you can use types as follows:

    def is_json(myjson):
        return type(myjson) == type({})
    
    def is_json_arr(myjson):
        return type(myjson) == type([{}])
    

提交回复
热议问题