How to update NFT metadata json object values

Hello, how can I add a new key value pair in a json object? I am uploading a list of NFT metadata to IPFS and I need to add a new key value pair with the IPFS url.

:computer: Environment

:memo:Details

{    "Name": "Solid Cat",
    "HP": "100",
    "Atk": "100",
    "Def": "100" ,
    "image:"https://gateway.pinata.cloud/ipfs/QmboVnnAr33bGwm5Gd68znRmhUximCabxkks6LDfJB445H"
}

Example Output:

{ 
    "Name": "Solid Cat",
    "HP": "100",
    "Atk": "100",
    "Def": "100" ,
    "image:"https://gateway.pinata.cloud/ipfs/QmboVnnAr33bGwm5Gd68znRmhUximCabxkks6LDfJB445H",    
"updateInfo":"https://gateway.pinata.cloud/ipfs/QmfWEMCu2YY9745wc2E9bvJUqeQMBVvRsAtZ7Ea9RjLyjR"
}

The code reads the values of various NFT metadata from anartwork json, loops and uploads each to IPFS and returns anuploaded.jsonwhich containes key value pairs of each IPFS hash to the original NFT metadata. Instead I just want theuploaded.json
to have same list of NFT data as inartwork.jsonplus theupdateInfokey and the IPFS url, which does not have to be pinata specifically

const ipfs = ipfsAPI({host: 'ipfs.infura.io', port: '5001', protocol: 'https' })
const main = async () => {
  let allAssets = {}
  console.log("\n\n Loading artwork.json...\n");
  const artwork = JSON.parse(fs.readFileSync('./artwork.json').toString())
  for(let a in artwork){
    console.log("  Uploading "+artwork[a].name+"...")
    const stringJSON = JSON.stringify(artwork[a])
    const uploaded = await ipfs.add(stringJSON)
    console.log("   "+artwork[a].name+" ipfs:",uploaded.path)
    allAssets[uploaded.path] = artwork[a]
  }
  console.log("\n Creating file with all the uploaded hashes...")
  fs.writeFileSync("./uploaded.json",JSON.stringify(allAssets))
};

Hi @Altaergo,

You may want to try StackExchange as this is more of a JavaScript question.

1 Like