Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json load problem
#2
Please, unless it is really big project, post your code in code tags, don't attach py files. See BBcode help for more info. This will make easier to answer and will attract more answers.

I added these for your this time.


Your problems comes every time when you use the same file name you append the json object to the file. As a result what you get is not A valid json file. e.g. after two 'runs' your json file will look like this
Output:
{"bar": "moo"}{"bar": "moo"}
you need to rethink your logic - e.g. load the json file and then append the new key-value pairs and then overwrite the existing file.

there are other problems with your code

1. you overwrite dname immediately
dname=str(input('select dictionary name: '))
dname= D()
first it is a string, a "name" and immediately you use same variable for D instance, so you lost the name

2. It is better to implement __str__() method instead of print_json()
see https://docs.python.org/3/reference/data...tomization

3. unless you write to file, before you print, you will not have _filename property set and this will rise an error. i.e. if you try to print the json, before you write it to file.
Reply


Messages In This Thread
json load problem - by mepyyeti - Dec-27-2017, 06:10 AM
RE: json load problem - by buran - Dec-27-2017, 07:32 AM
RE: json load problem - by mepyyeti - Dec-27-2017, 07:13 PM
RE: json load problem - by Larz60+ - Dec-27-2017, 08:32 PM
RE: json load problem - by mepyyeti - Dec-27-2017, 09:44 PM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:05 AM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON Dump and JSON Load foxholenoob 8 1,156 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Problem with nested JSON Kalet 7 2,821 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Problem With Database Calls and Load Timbo03 1 2,138 Nov-21-2021, 10:48 AM
Last Post: Timbo03
  Problem with Json Array kwekey 2 1,698 Aug-02-2021, 05:11 PM
Last Post: kwekey
  Problem to parse a json enigma619 3 2,402 Dec-04-2020, 08:16 AM
Last Post: enigma619
  Python - help with getting JSON from one DB and load to another DB qIekm 4 3,312 Apr-16-2020, 07:07 AM
Last Post: qIekm
  json problem enigma619 9 3,749 Dec-19-2019, 08:29 AM
Last Post: enigma619
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,922 Dec-11-2019, 07:50 AM
Last Post: parsley
  Output to a json file problem Netcode 3 3,759 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Load JSON file data into mongodb using pymongo klllmmm 1 11,891 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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