0

How to access the json object key/values abi and/or bin?

//File: myFile.json
{
    "contracts": {
        "ContractName1.sol:ContractName1": {
            "abi": "......",
            "bin": "......"
        },
        "ContractName2.sol:ContractName2": {
            "abi": "......",
            "bin": "......"
        },
    }
}

I can do the following thing:

const fs = require('fs')
const jsonFile = fs.readFileSync(process.cwd() + '/myFile.json')
const jsonObj = JSON.parse(jsonFile)
jsonObj.contracts.  // ??

Unfortunately it is not possible ContractName1.sol:ContractName1 e.g. like this:

jsonObj.contracts.ContractName1.sol:ContractName1.abi
sunwarr10r
  • 4,420
  • 8
  • 54
  • 109

1 Answers1

1

When accessing json you can use the dot notation or use the [] along with the name of the key inside of a string or a variable.

const json = {
  "contracts": {
    "ContractName1.sol:ContractName1": {
      "abi": "......",
      "bin": "......"
    },
    "ContractName2.sol:ContractName2": {
      "abi": "......",
      "bin": "......"
    },
  }
};

console.log(json.contracts['ContractName2.sol:ContractName2']);
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69