Home > front end >  json.decoder.JSONDecodeError When reading from a file
json.decoder.JSONDecodeError When reading from a file

Time:12-24

Hello I am trying to read data line by line from a file and then parse that data into json. each line is something like this

{"response":"OK","metadata_uri":"ipfs://bafkreicdoxeenpnlli52y6vhmx4tjis6ffelkzq5ujojyo22es2qbomfpq","name":"Happy Sad or Just Fine #14","description":"Perfect for you if you are happy sad or just fine.","file_url":"https://ipfs.io/ipfs/bafkreigcg6g5i47g3sc26lgk7h3zswtxma7qgs55lz4yyfiry3qgatgad4","external_url":null,"animation_url":null,"custom_fields":{"dna":"7b1a7e87d74ffd423c1ac59be1deace94916c6b9","edition":14,"date":1640190624930,"compiler":"HashLips Art Engine"},"attributes":[{"trait_type":"Backgrounds","value":"yellow ","max_value":null,"display_type":null},{"trait_type":"face","value":"red ","max_value":null,"display_type":null},{"trait_type":"eyes","value":"eyes crossed ","max_value":null,"display_type":null},{"trait_type":"mouth","value":"smirt right","max_value":null,"display_type":null}],"error":null}

However even though it is all valid json I get the error

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Here is my code

import json

f = open(r"C:\Users\wille\Desktop\code\cool_things\hashlips_art_engine-1.1.2_patch_v1\build\json/responce.text","r")

for i in range(1,1000):
    j = f.readline(i)
    j = json.loads(j)
    
    
    print(j)

Does anyone know how to solve this or what is causing it

CodePudding user response:

That's not how you use readline. readline(1) says "read at most one byte from the file", which of course screws up your JSON. Try this:

import json

f = open(r"C:\Users\wille\Desktop\code\cool_things\hashlips_art_engine-1.1.2_patch_v1\build\json/responce.text","r")

for line in f:
    j = json.loads(line)
    print(j)
  • Related