Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json decoding error
#1
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.
Reply
#2
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?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
@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)
Reply
#4
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.")
Reply
#5
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>
Reply
#6
I have Linux (LMDE5), it worked there with your 5 files. Maybe someone who uses Windows can help.
Reply
#7
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?
Reply
#8
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.")
Reply
#9
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.
Reply
#10
You can add all the files you need to the list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decoding lat/long in file name johnmcd 4 404 Mar-22-2024, 11:51 AM
Last Post: johnmcd
  json loads throwing error mpsameer 8 727 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Enigma Decoding Problem krisarmstrong 4 768 Dec-14-2023, 10:42 AM
Last Post: Larz60+
  python requests library .JSON() error mHosseinDS86 6 3,474 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,395 Nov-23-2022, 03:51 PM
Last Post: snippsat
  flask app decoding problem mesbah 0 2,375 Aug-01-2021, 08:32 PM
Last Post: mesbah
  Decoding a serial stream AKGentile1963 7 8,662 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,973 Jan-11-2021, 07:52 PM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,100 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  empty json file error mcmxl22 1 10,181 Jun-17-2020, 10:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020