Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json format
#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


Messages In This Thread
json format - by Bubu93200 - Apr-21-2022, 03:15 PM
RE: json format - by deanhystad - Apr-21-2022, 03:34 PM
RE: json format - by Bubu93200 - Apr-21-2022, 04:43 PM
RE: json format - by davediamond - Apr-21-2022, 03:51 PM
RE: json format - by deanhystad - Apr-21-2022, 05:08 PM
RE: json format - by Bubu93200 - Apr-21-2022, 05:17 PM
RE: json format JSONLINES for log or long files - by Bubu93200 - Apr-23-2022, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Json to table format python_student 4 15,404 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  format json outputs ! evilcode1 3 2,788 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Converting cells in excel to JSON format desmondtay 4 3,057 May-23-2022, 10:31 AM
Last Post: Larz60+
  Return in correct json format UtiliseIT 3 3,836 May-13-2019, 11:24 AM
Last Post: snippsat
  how do I format json data in splunk? fxtyom 14 17,292 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