OSError: [Errno 63] File name too long in JSON FILE (python)

Question:

i have this error – OSError: [Errno 63] File name too long:

what is the best way to work around this?

import requests
import json
import codecs

url = 'https://ratings.food.gov.uk/OpenDataFiles/FHRS314en-GB.json'
response = requests.get(url)

text = response.text

data = json.load(codecs.open(text, encoding='utf8'))

print('data', data)

Asked By: Code Ninja

||

Answers:

You could just leave out the first 3 Bytes, which indicate the BOM:

import requests
import json

url = 'https://ratings.food.gov.uk/OpenDataFiles/FHRS314en-GB.json'
response = requests.get(url)

text = response.content[3:]
data = json.loads(text)
print(data)

More dynamically would to lookup the correct BOM and strip if off the data:

BOMS = [
    codecs.BOM,
    codecs.BOM_BE,
    codecs.BOM_LE,
    codecs.BOM_UTF8,
    codecs.BOM_UTF16,
    codecs.BOM_UTF16_BE,
    codecs.BOM_UTF16_LE,
    codecs.BOM_UTF32,
    codecs.BOM_UTF32_BE,
    codecs.BOM_UTF32_LE,
]

url = 'https://ratings.food.gov.uk/OpenDataFiles/FHRS314en-GB.json'
response = requests.get(url)
data = response.content

for BOM in BOMS:
    if data.startswith(BOM):
        data = json.loads(data[len(BOM):])
        break
print(data)

Out:

{'FHRSEstablishment': {'Header': ...
Answered By: Maurice Meyer

codecs.open is used to open files. You used the entirety of the website’s JSON response as that file name. requests already knows about JSON and you can just have it do the decode for you

import requests
import json
import codecs

url = 'https://ratings.food.gov.uk/OpenDataFiles/FHRS314en-GB.json'
response = requests.get(url)
data = response.json()
print('data', data)

This is far better than making some unwarranted assumptions about how the JSON from this particular web site is encoded.

Answered By: tdelaney
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.