简体   繁体   中英

Use a value from one JSON to append in another using jq

Can some one please help me I have a nodekey file from which I want to take the address and append it in a genesis.json file.

This is the "nodekey file" . I want to fetch the address's value {4cb6046ae395bb6b5f9febc7bdcae28489268725} from this file and append it in the genesis.json file in nested key.

{"address":"4cb6046ae395bb6b5f9febc7bdcae28489268725","crypto":{"cipher":"aes-128-ctr","ciphertext":"04c9b4c56b717d2f64714de8aa9112c182eccc4e8e578b4c6d89b1621a3cd7c7","cipherparams":{"iv":"fca58569c4c864f9d9ac5953ee29f713"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"c2331c103e8a3321796fbaa79bcff301e22fa9b8cc4fb811ca0cbf96b48e697b"},"mac":"0656363e9828aa0f29215261c442169f33816156ce7d7ea795849c971a39fb7a"},"id":"3da32d09-f0b0-44a0-98ab-e9d03df7c952","version":3}

The below is the genesis file. In this file I want to append the address inside the alloc section at the end like this..

{
    "alloc": {
       "0xed9d02e382b34818e88b88a309c7fe71e65f419d": {
          "balance": "1000000000000000000000000000"
       },
       "0xca843569e3427144cead5e4d5999a3d0ccf92b8e": {
          "balance": "1000000000000000000000000000"
       },
       "0x0fbdc686b912d7722dc86510934589e0aaf3b55a": {
          "balance": "1000000000000000000000000000"
       },
       "0x9186eb3d20cbd1f5f992a950d808c4495153abd5": {
          "balance": "1000000000000000000000000000"
       },
       "0x0638e1574728b6d862dd5d3a3e0942c3be47d996": {
       "balance": "1000000000000000000000000000"
       }
       --want to add my address here
    },
    "coinbase": "0x0000000000000000000000000000000000000000",
        "config": {
        "homesteadBlock": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "chainId": 10,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "eip158Block": 0,
        "maxCodeSize": 35,
        "isQuorum": true
    },
   "difficulty": "0x0",
   "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
   "gasLimit": "0xE0000000",
   "mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
   "nonce": "0x0",
   "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
   "timestamp": "0x00"


}

I tried using jq .alloc += -- . But this replaces the last existing address in the alloc section.

You can use --argfile on the nodekey file, and use that later with the genesis.json file for appending

jq --argfile node nodekey.json '.alloc += { ($node.address): {balance: "1000000000000000000000000000"} }' genesis.json

Note that this will print the resulting output to the terminal. If you want the file to be modified in-place, follow the steps from Modify a key-value in a json using jq in-place .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM