Skip to content

Instantly share code, notes, and snippets.

@michaeljcoyne
Forked from jcurbelo/merge.py
Last active October 7, 2022 19:03
Show Gist options
  • Save michaeljcoyne/5dfc46e3d10a4408598c4764033ab121 to your computer and use it in GitHub Desktop.
Save michaeljcoyne/5dfc46e3d10a4408598c4764033ab121 to your computer and use it in GitHub Desktop.
Merge several metadata JSON files into one
import json
import os
FOLDER = './metadata'
result = []
# read all .json files in the folder
print('Reading files...')
json_files = [f for f in os.listdir(FOLDER) if f.endswith('.json')]
print('Found {} files.'.format(len(json_files)))
print('Merging...')
# sorts the files by their number
json_files.sort(key=lambda x: int(x.split('.')[0]))
for idx, f in enumerate(json_files):
with open(os.path.join(FOLDER, f)) as json_file:
data = json.load(json_file)
# sets a dummy image field
data['image'] = 'ipfs://YOUR_CID/' + str(idx + 1) + ".png";
result.append(data)
print('Writing output...')
# writes the result to a new file
with open('metadata.json', 'w') as outfile:
json.dump(result, outfile)
print('Done.')
@vonzy85
Copy link

vonzy85 commented Oct 7, 2022

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment