Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending json file
#1
Hi Guys,

I'm really new to python and just starting studying the basics of it. So please forgive me if I'm abit slow in understanding :)

So here is my issue, I have been trying to write a json file using python where the data comes from a API from our vendor. My issue is every time I append it, the file becomes an invalid json file.Using the json viewer, below is a sample of the json file. The API will only allow me to download max 100 data and then I need to use pagination to extract all data from the API. Hope someone can help me with it :)

{}-metadata
[]interactiondata
{}0
{}info
{}sdes
{}surveys
{}campaign
{}linescores
{}transcripts
{}visitorinfo
{}1
{}2
{}3
Reply
#2
You cannot add to the json file. You can collect all the information in memory in a single data structure and dump it to a file. If file already exists from previous runs of the script and you want to add to it, you need to load it first, update in memory and overwrite it.
Reply
#3
hi buran,
Thank you for the reply..how do it with python? Can you show me the correct way?
Reply
#4
As buran stated above, you need to load update and dump.
I have an example that does that, look at the customer file in
code here:
https://python-forum.io/Thread-ShoeBot-Problems?page=2 (Last post)
it is loaded at the beginning of the program, and saved whenever an update is
performed with the save_data method.
Reply
#5
thanks larz60 for the code. I will have to read and study it first before i can use it :) Btw what if i want to save multiple json files using python lets say for every page i read?
Reply
#6
you would just have to give each a name and dump the data.
You need to look at the json documents to get a feel for what is allowed and what is not.

below is a very basic document on how to save and retrieve json data.
see: https://code.tutsplus.com/tutorials/how-...-cms-25758

here on types of structures:
https://www.w3resource.com/JSON/structures.php

Depending on what you want to save, it may or may not be the right choice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 230 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 769 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,547 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,150 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,522 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,284 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Appending a row of data in an MS Excel file azizrasul 3 1,198 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Writing to json file ebolisa 1 1,016 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,505 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Print to a New Line when Appending File DaveG 0 1,229 Mar-30-2022, 04:14 AM
Last Post: DaveG

Forum Jump:

User Panel Messages

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