0

I want to parse a json file and print the data in console. I have a json file as input.json in which the content is :

{ "name":"John", "age":30, "city":"New York"}

Now I'm running a python script in the same directory where the input file is stored but getting the error. The python script looks like this :

 import json 

 # Opening JSON file 
 f = open('input.json','r') 
 data = json.load(f) 
 print(data)

Error is:

 File "C:\Users\madhav\Desktop\New folder (2)\instaclient\req.py", line 9, in <module>
data = json.load(f)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)   
  
balderman
  • 22,927
  • 7
  • 34
  • 52
Madhav mishra
  • 313
  • 4
  • 20
  • Why is this tagged with `javascript`? – Andreas Mar 02 '21 at 12:08
  • `import json with open('input.json') as f: data = json.load(f) print(data)` works with no issues – balderman Mar 02 '21 at 12:10
  • Is that your entire code? I suspect there's more code that reads the file and you're having the same problem as https://stackoverflow.com/questions/55305831/encounter-json-decoder-jsondecodeerror-expecting-value-line-1-column-1-char – Barmar Mar 02 '21 at 12:13
  • Yes I saw this code on any website I guess and tried to do the same thing but it gives me error @Barmar – Madhav mishra Mar 02 '21 at 12:26
  • I tried you code but still give me the same error @balderman – Madhav mishra Mar 02 '21 at 12:27
  • @MadhavMishra His code is the same as your code. Are you sure the file really contains the JSON and nothing else? – Barmar Mar 02 '21 at 12:27
  • Yes 100% sure, my code is working when i put the json content in the script and store it as a string like this x = ' ' ' { "name":"John", "age":30, "city":"New York"} ' ' ' with using this (' ' ') then json.load(x) @Barmar – Madhav mishra Mar 02 '21 at 14:57
  • I assume you mean `json.loads(x)`. If it works with a literal string and fails with the file, the problem must be with the file. What do you see if you do `data = f.read()`? – Barmar Mar 02 '21 at 17:31

0 Answers0