Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json format
#1
Hi,

I need your help, please. I'm french. Sorry for my bad english !

I want to append a file with json format but I want to have line carrier return in the file to simplify read of the file.

I have a file with json records.
example :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}]

I want to append the file with a new record :
addjson = {"surname" : "aaa", "forename" : "Paul", "age" : 40}

The code after gives :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}, {"surname" : "aaa", "forename" : "Paul", "age" : 40}]

but I have several problems :
1/ I would like to append the file with "addjson" record. At the moment, my code delete file and create a new file. How to append file with a new information ?

2/ I would like to format my json file in another form :
ListeCharacters.json =
[
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]

It's to simplfy reading of file. Is it allowed with json format ? how to do this ?

Thanks a lot for your help. Smile
############################################################################\n",
fileName = "ListeCharacters.json"

try:
    with open(fileName) as json_file:
        liste = json.load(json_file)
        liste.append(addjson)
except IOError:
    liste = [addjson]
   
with open(fileName, "w") as outfile:
    json.dump(liste, outfile)
Reply
#2
Load json, modify and dump is the correct method. You do not want to append the file.

You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
import json

data = [
    {"surname" : "xxx", "forename" : "jean", "age" : 24},
    {"surname" : "yyy", "forename" : "paul", "age" : 31},
    {"surname" : "zzz", "forename" : "julia", "age" : 28},
    {"surname" : "aaa", "forename" : "Paul", "age" : 40}
]

with open("test.json", "w") as outfile:
    json.dump(data, outfile, indent=4)
Output:
[ { "surname": "xxx", "forename": "jean", "age": 24 }, { "surname": "yyy", "forename": "paul", "age": 31 }, { "surname": "zzz", "forename": "julia", "age": 28 }, { "surname": "aaa", "forename": "Paul", "age": 40 } ]
Bubu93200 likes this post
Reply
#3
I'm busy turning a permanent storage list into an on-screen list, back in the day we would have said 'hard disk' instead of permanent storage.

Anyway, have you tried this?

# Python program to update
# JSON
 
 
import json
  
# JSON data:
x =  '{ "organization":"GeeksForGeeks",
        "city":"Noida",
        "country":"India"}'
 
# python object to be appended
y = {"pin":110096}
 
# parsing JSON string:
z = json.loads(x)
  
# appending the data
z.update(y)
 
# the result is a JSON string:
print(json.dumps(z))
Produces this output:
.{“pin”: 110096, “organization”: “GeeksForGeeks”, “country”: “India”, “city”: “Noida”} 
 
Reply
#4
(Apr-21-2022, 03:34 PM)deanhystad Wrote: Load json, modify and dump is the correct method. You do not want to append the file.

You should not worry about the format of your json data. Json files are easy to read, but are not designed to be human readable. You can make the file a little prettier using the indent argument.
import json

data = [
    {"surname" : "xxx", "forename" : "jean", "age" : 24},
    {"surname" : "yyy", "forename" : "paul", "age" : 31},
    {"surname" : "zzz", "forename" : "julia", "age" : 28},
    {"surname" : "aaa", "forename" : "Paul", "age" : 40}
]


with open("test.json", "w") as outfile:
    json.dump(data, outfile, indent=4)
Output:
[ { "surname": "xxx", "forename": "jean", "age": 24 }, { "surname": "yyy", "forename": "paul", "age": 31 }, { "surname": "zzz", "forename": "julia", "age": 28 }, { "surname": "aaa", "forename": "Paul", "age": 40 } ]

Thanks a lot.
This output format isn't enough compact.
I prefer this one :
data = [
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]

Is it allowed for json format ?
If yes, I will add code to obtain this format (not with json library).
Is there other format to obtain this type of output? I can make my format but I would like to use known format.
Reply
#5
I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.
menator01 likes this post
Reply
#6
(Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.


If I can load it with json library, it's allowed.
Ok. I'll try

Thanks for your help
Reply
#7
(Apr-21-2022, 05:17 PM)Bubu93200 Wrote:
(Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.


If I can load it with json library, it's allowed.
Ok. I'll try

Thanks for your help

(Apr-21-2022, 05:17 PM)Bubu93200 Wrote:
(Apr-21-2022, 05:08 PM)deanhystad Wrote: I do not think you can generate that output using json.dump(). If you want to know if it is allowed, try it yourself. Create a file using a text editor and try to load it.


If I can load it with json library, it's allowed.
Ok. I'll try

Thanks for your help

I solved my problem.
I exhibit my code for other people who can have the same problem.

I found code to have :
- compact file
- file easily readable by human
- file uses append mode so its very quick to open et upgrade file. Its very usable for very very long files (log files)
- file is breakable in several files
- we can easily convert file to json file
- we can add index or datation on records (add keys as "time", "date", "index" if you want)
- records can be differents on each line

And to do this, I use JSONLINES format (I didn't know this format)


import jsonlines

def writeJsonlines(fileName, dictionary):
    """
    https://jsonlines.org/
    use import jsonlines
    """ 
    try:
        with jsonlines.open(fileName, "a") as jsonlinesfile:
            jsonlinesfile.write(dictionary)
    except IOError:
        print("writeJsonlines : failed to open file") 

def readJsonlines(fileName):
    """
    https://jsonlines.org/
    use import jsonlines
    """ 
    try:
        with jsonlines.open(fileName, 'r') as jsonlfile:
            jsonl_list = list(jsonlfile)
        print('readjsonlines : ', jsonl_list, "\n")
        return jsonl_list
    except IOError:
        print("readJsonlines : failed to open file")
        return None
        
File output is as :
Output:
{"surname": "xxx", "forename": "jean", "age": 24}, {"surname": "yyy", "forename": "paul", "age": 31}, {"surname": "zzz", "forename": "julia", "age": 28}, {"surname": "aaa", "forename": "Paul", "age": 40}, {"name": "bruno", "age": 46}
Very usefull, flexible. great
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,761 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Convert Json to table format python_student 2 5,576 Sep-28-2022, 12:48 PM
Last Post: python_student
  Converting cells in excel to JSON format desmondtay 4 1,761 May-23-2022, 10:31 AM
Last Post: Larz60+
  Return in correct json format UtiliseIT 3 2,945 May-13-2019, 11:24 AM
Last Post: snippsat
  how do I format json data in splunk? fxtyom 14 13,960 May-24-2017, 09:23 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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