Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors using json file
#1
I been trying to build a program to use the OpenWeatherAPI with Tkinter. Things work well if I'm using live data. But I have one data set that provides national weather alert information saved in a json file.

When I read the json file in for some reason it is converted to a sting instead of leaving it as a dictionary. I have tried rewriting the code but the results are the same. You can see the error in the attached PDF file.

The data in the file is in dictionary format but when I try to import it something adds a "'", single quote to the beginning and end of the file. Thus changing from a dictionary to a string and my code to access the dictionary fields fails.

Any suggestions are greatly appreciated.

Ron R

Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] on linux via Visual Studio Code
likes this post

Attached Files

.zip   JsonErrorArchive.zip (Size: 144.22 KB / Downloads: 41)
Reply
#2
Post all the code, there are some things missing.
'Ts_to_Dt' is not defined
Reply
#3
Try to post code here,or a place that can look without unpack a .zip file.
Yes you’re turning your dict back into a string.
json.dumps(data_in) which serializes it back to a JSON string.
Don't use global(usually never),just return data out.
import json from pathlib import Path def json_to_dict(fn): '''convert json file to python dictionary''' filename = 'OW_JamaicaAlert.json' with open(filename, encoding="utf-8") as file: # dict, keep it as-is (no dumps!) data = json.load(file) return data data = json_to_dict("OW_JamaicaAlert.json")
Test.
>>> data['daily'][0] {'clouds': 81, 'dew_point': 296.52, 'dt': 1761235200, 'feels_like': {'day': 305.83, 'eve': 306.66, 'morn': 298.11, 'night': 296.82}, 'humidity': 88, 'moon_phase': 0.07, 'moonrise': 1761223500, 'moonset': 1761264000, 'pop': 1, 'pressure': 1011, 'rain': 15.3, 'summary': 'Expect a day of partly cloudy with rain', 'sunrise': 1761217381, 'sunset': 1761259181, 'temp': {'day': 300.91, 'eve': 300.97, 'max': 305.12, 'min': 296, 'morn': 297.12, 'night': 296}, 'uvi': 9.6, 'weather': [{'description': 'moderate rain', 'icon': '10d', 'id': 501, 'main': 'Rain'}], 'wind_deg': 2, 'wind_gust': 2.95, 'wind_speed': 1.77} >>> data['daily'][0]['feels_like'] {'day': 305.83, 'eve': 306.66, 'morn': 298.11, 'night': 296.82}
RonR likes this post
Reply
#4
(Oct-27-2025, 08:05 PM)snippsat Wrote: Try to post code here,or a place that can look without unpack a .zip file.
Yes you’re turning your dict back into a string.
json.dumps(data_in) which serializes it back to a JSON string.
Don't use global(usually never),just return data out.
import json from pathlib import Path def json_to_dict(fn): '''convert json file to python dictionary''' filename = 'OW_JamaicaAlert.json' with open(filename, encoding="utf-8") as file: # dict, keep it as-is (no dumps!) data = json.load(file) return data
**************************************
Thank you very much for your help. For some reason I thought json.load and json.dump needed to be used together. The light came on when I got into the area of my program that I need to finish. National Weather Alerts don't happen very often here so I was chasing them around the Caribbean and finally got one for Kingston, Jamaica.

In the future I will try to post better code and include it with the text. I guess I took the forum's suggestions to heart and only included what demonstrated the problem.

Ron R
likes this post
Reply
#5
(Oct-27-2025, 07:54 PM)Axel_Erfurt Wrote: Post all the code, there are some things missing.
'Ts_to_Dt' is not defined

Thank you for replying I posted the way I thought the forum wanted it done. Enough code to demonstrate the problem. And I did not think anyone would want to wade through all that for this problem.
Reply
#6
(Oct-28-2025, 01:06 AM)RonR Wrote: Thank you for replying I posted the way I thought the forum wanted it done. Enough code to demonstrate the problem. And I did not think anyone would want to wade through all that for this problem.
That's correct and your code was sufficient to see the problem (rather obvious one) even without running it. That said, I think @Axel_Erfurt comment was more in direction it should be reproducible, i.e. we should be able to run it. And that is not possible when there is NameError and more often than not we would want to run your code and reproduce the problem.

Also, please, don't attach code as zip files and images of code/erorrs/output. Post the code/errors/output in the body of the post as properly formatted text, using respective BBCode tags. In case of large project, multiple py files, etc. it's better to post a link to a repo (e..g on GitHub. GitLab, etc.).
likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors during run .exe file generated with Pyinstaller BushyAxis793 4 5,230 Mar-25-2025, 05:57 PM
Last Post: BushyAxis793
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,207 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  encrypt data in json file help jacksfrustration 1 3,477 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 2,951 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 6,289 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 7,408 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 1,928 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Converting a json file to a dataframe with rows and columns eyavuz21 13 20,212 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 2,987 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 2,197 Jul-17-2022, 04:51 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.