Skip to content

Instantly share code, notes, and snippets.

@dreamerkumar
Created September 14, 2017 14:29
Show Gist options
  • Save dreamerkumar/3c0d64f1dff8bd06e0eabe4f891d3424 to your computer and use it in GitHub Desktop.
Save dreamerkumar/3c0d64f1dff8bd06e0eabe4f891d3424 to your computer and use it in GitHub Desktop.
Python script to change the structure within json files
import json
def updateJsonFile(fileName):
jsonFile = open(fileName, "r") # Open the JSON file for reading
data = json.load(jsonFile) # Read the JSON into the buffer
jsonFile.close() # Close the JSON file
for data1 in data:
if 'property1' in data1:
property2 = {}
if 'property2' in data1:
property2 = data1['property2']
data1['associate'] = {
'property1': data1['property1'],
'property2': property2
}
if 'property1' in data1:
del data1['property1']
if 'property2' in data1:
del data1['property2']
## Save our changes to JSON file
jsonFile = open(fileName, "w+")
jsonFile.write(json.dumps(data, indent=2, sort_keys=True))
jsonFile.close()
file_list = [
'filePath1',
'filePath2'
]
for file_name in file_list:
updateJsonFile(file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment