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
#2
don't forget that:
for file_path in os.listdir(path):
will get you everything in path, including directories
Can't run your code as is, so don't know if this is an issue.
It could be in the future, so still not a good idea.

if running python 3.5 or newer, you can use pathlib (untested code):
>>> from pathlib import Path
>>> path Path(C:/Users/HOLLEYP/Downloads/TSS_DonkeyCarChallenge/tubs/tubs_cleaned/data/data_aug_test/)
>>> files = [x for x in home.iterdir() if x.is_file()]
>>> for file_path in files:
...     do stuff here
>>>
Reply
#3
OK. Thanks. Got it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 832 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  HELP! Importing json file into csv into jupyter notebook vilsef 2 2,575 Jan-22-2021, 11:06 AM
Last Post: snippsat
  Python - Pandas writing blank files to file tdunphy 0 1,999 Jan-14-2021, 12:11 AM
Last Post: tdunphy
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,246 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  JSON file Loading issue punna111 4 8,594 Jun-29-2020, 08:07 AM
Last Post: buran
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,943 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  Help batch converting .json chosen file to MySQL BrandonKastning 2 2,327 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  reading tab file Mandiph 1 2,187 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,146 Aug-23-2019, 03:04 AM
Last Post: Plastefuchs84
  Reading a .dat file in Python mohd_umair 4 23,561 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