Python Forum
reading, modifying and writing json file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading, modifying and writing json file
#1
Hi Python Experts,

I am trying to open a json file, modify the content and write it to a different directory.
The code runs but nothing is really happening.

I am not sure what should come first, the dump or the misc save. Is the dump a save as well?

from scipy import ndimage, misc
import numpy as np
import os
import cv2
import json

def main():
    outPath='C:/..../'
    path='C:/...../data_aug_test/'

    # iterate through the names of contents of the folder
    for file_path in os.listdir(path):
	
	#create the full input path and read the file
        input_path = os.path.join(path, file_path)
	
        if file_path.endswith('.jpg'):	

             print('jpeg process')
		
    else: #.json
         print('else json handling')
		 
         with open(input_path, 'r') as f:
             data = json.load(f)
             data['id'] = 134 # <--- modify value.
			 
		    #write file then save	 
             jsonsavepath = os.path.join(outPath, 'flipped_'+file_path)
             misc.imsave(jsonsavepath, f)
			 
         with open(input_path, 'w') as f:
             json.dump(data, f, indent=4)	 
		 
if __name__ == '__main__':
     main()
Reply


Messages In This Thread
reading, modifying and writing json file - by metalray - Jun-06-2018, 09:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 838 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  HELP! Importing json file into csv into jupyter notebook vilsef 2 2,588 Jan-22-2021, 11:06 AM
Last Post: snippsat
  Python - Pandas writing blank files to file tdunphy 0 2,006 Jan-14-2021, 12:11 AM
Last Post: tdunphy
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,263 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  JSON file Loading issue punna111 4 8,617 Jun-29-2020, 08:07 AM
Last Post: buran
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,956 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  Help batch converting .json chosen file to MySQL BrandonKastning 2 2,352 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  reading tab file Mandiph 1 2,200 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  save my sensor data from the bme680 into a json or csv file Plastefuchs84 1 3,151 Aug-23-2019, 03:04 AM
Last Post: Plastefuchs84
  Reading a .dat file in Python mohd_umair 4 23,604 Apr-24-2019, 12:07 PM
Last Post: mohd_umair

Forum Jump:

User Panel Messages

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