Issues reading json from txt file
Question: I have a json string in a txt file and I'm trying to read it to do some other
procedures afterwards. It looks like this:

    
    
    with open('code test.txt', 'r', encoding=('UTF-8')) as f:
        x = json.load(f)
    

I know the json is valid, but I'm getting:

    
    
    Traceback (most recent call last):
      File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
        obj, end = self.scan_once(s, idx)
    StopIteration
    
    During handling of the above exception, another exception occurred:
    
        Traceback (most recent call last):
          File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
            user_input()
          File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
            child_remover()
          File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
            x = json.load(f)
          File "C:\Python33\lib\json\__init__.py", line 274, in load
            parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
          File "C:\Python33\lib\json\__init__.py", line 319, in loads
            return _default_decoder.decode(s)
          File "C:\Python33\lib\json\decoder.py", line 352, in decode
            obj, end = self.raw_decode(s, idx=_w(s, 0).end())
          File "C:\Python33\lib\json\decoder.py", line 370, in raw_decode
            raise ValueError("No JSON object could be decoded")
        ValueError: No JSON object could be decoded
    

I used this [website](http://jsonlint.com/) to check if the string is valid.
If I use `.loads()`, I get a different error:

    
    
    Traceback (most recent call last):
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
        user_input()
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
        child_remover()
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
        x = json.loads(f)
      File "C:\Python33\lib\json\__init__.py", line 319, in loads
        return _default_decoder.decode(s)
      File "C:\Python33\lib\json\decoder.py", line 352, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    TypeError: expected string or buffer
    

Originally the json was embeded in my script like this:

    
    
    json_text="""json stuff here"""
    

And didn't get any errors. Any ideas on how to fix this???

Running python 3.3.3 just in case.

Thanks!!

**EDIT:**

Just some random (valid) json on the txt and I get the same issue. This os one
of the ones i tried:

    
    
    {"data":
        {"mobileHelp":
            {"value":
                {
                "ID1":{"children": [1,2,3,4,5]},
                "ID2":{"children": []},
                "ID3":{"children": [6,7,8,9,10]}
                }
            }
        }
    }
    

Which is valid as well as per jsonlint.com.


Answer: Your file contains a [UTF-8 BOM
character](http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) at the start.
UTF-8 **doesn't need a BOM** but especially Microsoft tools insist on adding
one anyway.

Open the file with the `utf-8-sig` encoding instead:

    
    
    >>> open('/tmp/json.test', 'wb').write(b'\xef\xbb\xbf{"data":\r\n    {"mobileHelp":\r\n        {"value":\r\n            {\r\n            "ID1":{"children": [1,2,3,4,5]},\r\n            "ID2":{"children": []},\r\n            "ID3":{"children": [6,7,8,9,10]}\r\n            }\r\n        }\r\n    }\r\n}')
    230
    >>> import json
    >>> with open('/tmp/json.test', encoding='utf8') as f:
    ...     data = json.load(f)
    ... 
    Traceback (most recent call last):
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 367, in raw_decode
        obj, end = self.scan_once(s, idx)
    StopIteration
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 271, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 316, in loads
        return _default_decoder.decode(s)
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 351, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 369, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    >>> with open('/tmp/json.test', encoding='utf-8-sig') as f:
    ...     data = json.load(f)
    ... 
    >>> data
    {'data': {'mobileHelp': {'value': {'ID2': {'children': []}, 'ID3': {'children': [6, 7, 8, 9, 10]}, 'ID1': {'children': [1, 2, 3, 4, 5]}}}}}
    

Note that from Python 3.4 onwards you get a more helpful error message here:

    
    
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 268, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 314, in loads
        raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
    ValueError: Unexpected UTF-8 BOM (decode using utf-8-sig)
    

