Update NFT JSON Metadata with Python

If you have ever tried to deploy a huge NFT project, you’ve come to realize the importance of having a reliable script to update the metadata, especially since your metadata will not be complete until you have committed the image folder to IPFS and retrieved the hash for the base URI. But what about when you want to change the metadata: perhaps add new categories or delete old ones? I’ve got a simple script that only requires Python.

import os

In your .py file, all you need is to import os.

The ‘listdir’ function creates an array. So now, we just need to traverse the array with a for loop.

‘rb’ stands for ‘read bytes,’ which is necessary in order to read lines in a JSON file.

Python has a built-in replace function, where the first parameter is the input to be searched for and replaced by the second input. You need to convert the string to bytes and specify the encoding.

Open the file again and write the changes to it.

Full code:

You can replace whichever criteria you want.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Law, programming, and everything in-between! Coming up with fun coding projects with real-world application.