Python Forum
Exporting Json events to a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exporting Json events to a text file
#1
Hi, I was given a Python script from a vendor to pull down events from their API. The script works but I need to save these events to a file and not just view them. I'm not that familiar with Python but have a general understanding of programming. When I run the code below, it generates my text file but it only saves the last event pulled from the API and oddly saves two entries for it in the text file. In the code, I see a while loop and for loop and I think my json.dump command is inside both loops and therefore creating two entries in the text file. I've tried moving it outside each loop but not dice.

Any ideas would be appreciated!

    while True:
        data = export_events(token, event_type, batch_size, start_date, end_date, client_id, client_secret)

        total_events = total_events + len(data)
        if data is None:
            break
        elif len(data) == 0:
            break
        else:
		        for event in data:
    			    with open('jsonfile.txt','w') as outfile:
				                   json.dump(data,outfile, indent=2, sort_keys=True)  
			        print json.dumps(data, indent=2, sort_keys=True)
Reply


Messages In This Thread
Exporting Json events to a text file - by uklipse - Aug-21-2018, 01:31 PM
RE: Exporting Json events to a text file - by buran - Aug-21-2018, 01:40 PM
RE: Exporting Json events to a text file - by buran - Aug-21-2018, 02:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 224 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 764 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,536 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,147 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,510 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,131 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  validate large json file with millions of records in batches herobpv 3 1,280 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,738 Aug-01-2022, 06:08 PM
Last Post: paulyan
  Writing to json file ebolisa 1 1,012 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,495 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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