Python Forum

Full Version: json decoding error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello my friends,
Wish you al have a restful sunday.
I want to use one json file in python with a following code.

import json
import datetime
import pandas as pd
list=[]
with open("12.json", encoding="UTF-8") as f:
    jsondata = json.load(f)
for matches in jsondata["items"]:
    if "Alternative" in matches["champName"]:
        continue
    list.append([matches["champName"],datetime.datetime.fromtimestamp(matches["dateStart"]),matches["opp1"],matches["opp2"],matches["score"]])
df=pd.DataFrame(list)
df.to_excel("output.xlsx")
print("Saved.")
when I run it, it gives such error.
Error:
Traceback (most recent call last): File "c:\Users\monst\Downloads\python_stavka\parcali_results\parcali_results-FilterExcel-dateupdate.py", line 8, in <module> jsondata = json.load(f) File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 293, in load return loads(fp.read(), File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 15 column 1 (char 1472190)
How can I fix that?
Thank you.
Not that I've done so much coding with JSON files, but on the face of this, the first question I have: are you sure that the JSON file is valid?

Maybe you could post it up? Or is it HUGE?
@rob101
thanks a lot for your quick response pal.
Regarding my some healths problems in my waist, i couldn't use pc since sunday and still not recovered.

Yeah you are correct my friend.
I wanted to merge five singles json files manually bcuz of my insufficient programming knowledge.
and merged it wrongly. Bcuz as you guessed, I run the code per single json file and it executed without any errors.

I wish you could help me to merge "items" object of the single json files linked in the below.

1.json
2.json
3.json
4.json
5.json
12.json (merged 1.2.3.4.5.json)
try this

import json
import datetime
import pandas as pd
list=[]

# combine json
files=['1.json', '2.json', '3.json', '4.json', '5.json']

result = []
for f in files:
    with open(f, "r") as infile:
        result.append(json.load(infile))

with open("12.json", "w") as outfile:
    json.dump(result, outfile)


# make xlsx
with open("12.json", encoding="UTF-8") as f:
    jsondata = json.load(f)
    
for data in jsondata:
    if "items" in data:
        for matches in data["items"]:
            if "Alternative" in matches["champName"]:
                continue
            list.append([matches["champName"],datetime.datetime.fromtimestamp(matches["dateStart"]),matches["opp1"],matches["opp2"],matches["score"]])
df=pd.DataFrame(list)
df.to_excel("output.xlsx")
print("Saved.")
Hey @Axel_Erfurt,
Thanks a lot for your interest.
I deleted existing 12.json in the folder and Run your code, but it gave following error.
Error:
Traceback (most recent call last): File "c:\post4_testRun\post6_code.py", line 12, in <module> result.append(json.load(infile)) File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 293, in load return loads(fp.read(), File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\encodings\cp1254.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 9680: character maps to <undefined>
I have Linux (LMDE5), it worked there with your 5 files. Maybe someone who uses Windows can help.
i see Mr. @Axel_Erfurt

files=['1.json', '2.json', '3.json', '4.json', '5.json']
what about this part?
how this part can be edited to merge all *.json files in same directory?
That's only a list of the file names.

The problem is at

result.append(json.load(infile))

try open with encoding

import json
import datetime
import pandas as pd
list=[]
 
# combine json
files=['1.json', '2.json', '3.json', '4.json', '5.json']
 
result = []
for f in files:
    with open(f, "r", encoding="utf-8") as infile:
        result.append(json.load(infile))
 
with open("12.json", "w") as outfile:
    json.dump(result, outfile, ensure_ascii=False)
 
 
# make xlsx
with open("12.json", encoding="UTF-8") as f:
    jsondata = json.load(f)
     
for data in jsondata:
    if "items" in data:
        for matches in data["items"]:
            if "Alternative" in matches["champName"]:
                continue
            list.append([matches["champName"],datetime.datetime.fromtimestamp(matches["dateStart"]),matches["opp1"],matches["opp2"],matches["score"]])
df=pd.DataFrame(list)
df.to_excel("output.xlsx")
print("Saved.")
it gave same error
(Mar-22-2023, 05:36 PM)Axel_Erfurt Wrote: [ -> ]That's only a list of the file names.
i know.
I wanted to marge not only five json files, thare are tens of json files in same directory.
So I wanted to edit that part too.
Anyway.
You can add all the files you need to the list.
Pages: 1 2