-
-
Save michaeljcoyne/5dfc46e3d10a4408598c4764033ab121 to your computer and use it in GitHub Desktop.
Merge several metadata JSON files into one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done.