Edit Your NFT Metadata Files in Bulk With Python

K. N
2 min readFeb 27, 2022
(Image Source)

Developing and launching an NFT collection has definitely been a learning experience. One of the crucial component of any NFT collection is the .json metadata file. Usually, they are generated in bulk and I wrote about it here. However, once you generate them, you may want to add or edit something in the file and that means updating all the metadata files in your collection. It can be easily accomplished in python.

You can think of the .json file as a dictionary. In order to add or update a field, you use the same code as you would for a dictionary. Following is a code snippet with comments that will load .json files in a directory one by one, update each file and same it.

folder = 'metadata' #the folder containing the metadata files
field = 'image' #the field you want to add or edit
new_value = 'test url' #value of the field
#import necessary libraries
import json
import os
for file in os.listdir(folder):
with open(folder+'/'+ file, 'r+') as f:
data = json.load(f)
data[field] = new_value
f.seek(0) #should reset file position to the beginning.
json.dump(data, f, indent=4)
f.truncate() # remove remaining part

Make sure there are no files other than .json files otherwise you will get an error when the code reaches that file. That’s it! It was simple as that.

--

--