Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 106 lines (68 sloc) 2.3 KB
#/usr/bin/env python3
Author: Nate Browne
Last Edited: 28 February 2018
Takes two .json files and merges their contents into one .json file.
import json
from sys import argv, exit
Takes passed in .json files and merges them into one file
Exceptions Thrown: OSError: Thrown if an OSError or DecodeError are caught in
attempting to open and parse the given filename.
Params: file1 - first json file to merge
file2 - second json file to merge
destfile - destination file to write to
Return: None.
def merge_json(file1, file2, destfile):
print('Opening file 1...')
with open(file1, 'r') as inputfile:
data1 = json.load(inputfile)
print('Opening file 2...')
with open(file2, 'r') as secondinput:
data2 = json.load(secondinput)
new_data = data1 + data2
print('Writing to new file...')
with open(destfile, 'w') as outputfile:
json.dump(new_data, outputfile)
except(OSError, json.decoder.JSONDecodeError):
print('File invalid (either doesn\'t exist or can\'t be read).')
# Raise error for calling function to handle
raise OSError
Parses command line argument and handles errors, then delegates to other
function to merge the files
Params: None.
Return: None.
def main():
# Check command line argument number for correctness. Should only be 3
if len(argv) != 4:
print('USAGE: ./%s <file1> <file2> <destinationfile>' % argv[0])
# Parse arguments
file1 = argv[1]
file2 = argv[2]
destination = argv[3]
# Make sure file1 passed in ends with .json
if '.json' not in file1:
file1 += '.json'
# Make sure file2 passed in ends with .json
if '.json' not in file2:
file2 += '.json'
# Make sure file1 passed in ends with .json
if '.json' not in destination:
destination += '.json'
# Merge the files
merge_json(file1, file2, destination)
print('Problem with I/O for file. Exiting...')
# Standard boilerplate to run the main function
if __name__ == '__main__':