-2

I have a json file.

with open('list.json', "r") as f:
    r_list = json.load(f)

crashes with:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0

I checked the schema online and the schema works.

The schema is very simple:

{"foo": [
{"name": "AAA\u2019s BBB CCC", "url": "/foome/foo"}
]}

Tried to play with:

  • file encoding
  • Try a dummy file

.. run out of ideas - is it something where ´json.load´ expects a binary?


Edit 1 Code works in a plain file, does not work in the scrapy class

import scrapy
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
import json

class myScraper(scrapy.Spider):
    name="testScraper"

    def start_requests(self):        
        with open('test.json') as f:
            self.logger.info(f.read()) #shows the file content
            r_list = json.load(f) # breaks with the error msg
            yield "foo"

    def parse(self, response):
        self.logger.info("foo")

'test.json'

{
    "too": "foo"
}
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69

1 Answers1

-2

Most likely your file is empty.

Example:

https://repl.it/@mark_boyle_sp/SphericalImpressiveIrc


updated:

Your iterator is exhausted as also discussed in the comments. Since you log the files contents the iterator is at the end of the file. (looks like an empty file, hence the exception)

Reset the iterator or read the contents to a local value and operate on that.

json_str = f.read()
self.logger.info(json_str) #shows the file content
r_list = json.loads(json_str)

updated again

(I assume) The scrapy issue you are having is in the parse method? The response body is a bytes object you will need to decode it and use loads on the resulting string like so :

def parse(self, response):
    self.logger.info("foo")
    resp_str = response.body.decode('utf-8') 
    self.logger.info(resp_str) #shows the response
    r_list = json.loads(json_str)
corn3lius
  • 4,857
  • 2
  • 31
  • 36