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

后端 未结 4 1066
南旧
南旧 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条回答
  • I would say parsing it is the only way you can really entirely tell. Exception will be raised by python's json.loads() function (almost certainly) if not the correct format. However, the the purposes of your example you can probably just check the first couple of non-whitespace characters...

    I'm not familiar with the JSON that facebook sends back, but most JSON strings from web apps will start with a open square [ or curly { bracket. No images formats I know of start with those characters.

    Conversely if you know what image formats might show up, you can check the start of the string for their signatures to identify images, and assume you have JSON if it's not an image.

    Another simple hack to identify a graphic, rather than a text string, in the case you're looking for a graphic, is just to test for non-ASCII characters in the first couple of dozen characters of the string (assuming the JSON is ASCII).

    0 讨论(0)
  • 2020-12-07 10:28

    I came up with an generic, interesting solution to this problem:

    class SafeInvocator(object):
        def __init__(self, module):
            self._module = module
    
        def _safe(self, func):
            def inner(*args, **kwargs):
                try:
                    return func(*args, **kwargs)
                except:
                    return None
    
            return inner
    
        def __getattr__(self, item):
            obj = getattr(self.module, item)
            return self._safe(obj) if hasattr(obj, '__call__') else obj
    

    and you can use it like so:

    safe_json = SafeInvocator(json)
    text = "{'foo':'bar'}"
    item = safe_json.loads(text)
    if item:
        # do something
    
    0 讨论(0)
  • 2020-12-07 10:38

    Example Python script returns a boolean if a string is valid json:

    import json
    
    def is_json(myjson):
      try:
        json_object = json.loads(myjson)
      except ValueError as e:
        return False
      return True
    

    Which prints:

    print is_json("{}")                          #prints True
    print is_json("{asdf}")                      #prints False
    print is_json('{ "age":100}')                #prints True
    print is_json("{'age':100 }")                #prints False
    print is_json("{\"age\":100 }")              #prints True
    print is_json('{"age":100 }')                #prints True
    print is_json('{"foo":[5,6.8],"foo":"bar"}') #prints True
    

    Convert a JSON string to a Python dictionary:

    import json
    mydict = json.loads('{"foo":"bar"}')
    print(mydict['foo'])    #prints bar
    
    mylist = json.loads("[5,6,7]")
    print(mylist)
    [5, 6, 7]
    

    Convert a python object to JSON string:

    foo = {}
    foo['gummy'] = 'bear'
    print(json.dumps(foo))           #prints {"gummy": "bear"}
    

    If you want access to low-level parsing, don't roll your own, use an existing library: http://www.json.org/

    Great tutorial on python JSON module: https://pymotw.com/2/json/

    Is String JSON and show syntax errors and error messages:

    sudo cpan JSON::XS
    echo '{"foo":[5,6.8],"foo":"bar" bar}' > myjson.json
    json_xs -t none < myjson.json
    

    Prints:

    , or } expected while parsing object/hash, at character offset 28 (before "bar}
    at /usr/local/bin/json_xs line 183, <STDIN> line 1.
    

    json_xs is capable of syntax checking, parsing, prittifying, encoding, decoding and more:

    https://metacpan.org/pod/json_xs

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题