Last active
February 21, 2023 23:35
-
-
Save jcurbelo/f683c7434693c3c7b4c0e3a532e10bf4 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.') |
@michaeljcoyne thank you so much for noticing this! you are right, this interpolation syntax was added in python 3.6 see this SO answer. Updated my gist thanks to you! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I removed the f from that line and it works
https://gist.github.com/michaeljcoyne/5dfc46e3d10a4408598c4764033ab121/revisions