4

I have a bunch of JSON files in a directory that I'm uploading to IPFS, to be used as NFT metadata. The problem is that after I upload them, the URI to access them looks like this: ipfs://<directory_hash>/<tokenID>.json.

This is a problem because the tokenURI function in the standard ERC721 implementation simply appends the token ID to baseURI. This means each token's URI would have to look like this: ipfs://<directory_hash>/<tokenID>, without the .json file extension.

I know I could just override this function or simply edit it, but I'd rather not. Especially because I've seen other NFT projects successfully do what I'm trying to do but can't seem to figure out.

So, when uploading JSON files in a directory to IPFS, how can I prevent the .json extension from being part of the URI?

1
  • How did you manage to create directories in IPFS?!
    – PedroD
    Nov 9, 2021 at 20:28

3 Answers 3

1

I would suggest simply removing the .json extension from the files and uploading them to ipfs like that. They will function all the same, but will the URIs will then resolve correctly

2
  • 1
    Stupid question, but how would I do this exactly? I'm on MacOS. If I rename the files in Finder, they still show up with the .json extension in terminal. If I rename them and remove the extension with mv then they are converted to text files. Aug 28, 2021 at 22:56
  • 1
    Never mind I figured it out... For some reason it didn't occur to me that JSON files were interchangeable with text files :/ Aug 30, 2021 at 12:30
1

Thanks to the replies guys, I almost had an heart attack with this .json file extension removal situation.

If your smart contract code has string public baseExtension = “.json”, you won’t have a problem when your TokenURI is: IPFS://Hash, since the TokenURI will ping IPFS://Hash/TokenID.json as set out in your code.

If your smart contract code does not have string public baseExtension = “.json”, then you should open your Mac’s Finder -> Preferences -> Check “Show all filename extensions” and uncheck “Show warning before changing an extension”.

Then going back to your NFT Metadata folder, select all of your .JSON files, then select “Rename”.

In “Rename”, select “Replace Text”, in “Find” type .json, in “Replace with do not input anything. Then click “Rename button”

You can now upload your files to IPFS without the .json extension in the end, and your TokenURI will appear neatly as ipfs://hash/tokenID.

https://pumpametti.medium.com/how-to-upload-nft-metadata-to-ipfs-without-the-json-file-extension-65c1d5d5562f

0

I had the same problem. I solved it by overriding the tokenURI function to append the .json extension:

return string(abi.encodePacked(base, tokenId, ".json"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.