Home > other >  How to Select only a couple keys and keep it in an array format using jq
How to Select only a couple keys and keep it in an array format using jq

Time:11-23

I have the following json I want to sort through:

{
  "unbonding_responses": [
    {
      "delegator_address": "tori1zkzfu5tqee0p698w3g83x8a2zswgqglj4t8z3j",
      "validator_address": "torivaloper189r2mx8h5zt7ep4cf0s4np79w38l420na3h6sa",
      "entries": [
        {
          "creation_height": "753019",
          "completion_time": "2022-12-13T16:00:56.664864370Z",
          "initial_balance": "3000000",
          "balance": "3000000"
        }
      ]
    },
    {
      "delegator_address": "tori1rjg0vg9hlu8kygnrdehzt3z0t4ea5ssu6wkm06",
      "validator_address": "torivaloper189r2mx8h5zt7ep4cf0s4np79w38l420na3h6sa",
      "entries": [
        {
          "creation_height": "653923",
          "completion_time": "2022-12-07T00:53:48.247870844Z",
          "initial_balance": "50000000",
          "balance": "50000000"
        }
      ]
    },
    {
      "delegator_address": "tori1aw44evl5zlqtw93zln83tp4v3w4kp6e9zmx64k",
      "validator_address": "torivaloper189r2mx8h5zt7ep4cf0s4np79w38l420na3h6sa",
      "entries": [
        {
          "creation_height": "597161",
          "completion_time": "2022-12-03T05:32:59.107845572Z",
          "initial_balance": "2000000",
          "balance": "2000000"
        }
      ]
    }
  ],
  "pagination": {
    "next_key": null,
    "total": "3"
  }
}

Currently I am filtering using the following command

 curl -s https://rest.mainnet.teritori.com/cosmos/staking/v1beta1/validators/torivaloper1qy38xmcrnht0kt5c5fryvl8llrpdwer6atxj5u/unbonding_delegations | jq '.unbonding_responses[].entries[]|.balance|=(tonumber | ./1000000)'

Which gives the following results:


{
  "creation_height": "546726",
  "completion_time": "2022-11-29T20:41:54.102529873Z",
  "initial_balance": "1000000",
  "balance": 1
}
{
  "creation_height": "560257",
  "completion_time": "2022-11-30T18:22:32.286053755Z",
  "initial_balance": "4000000",
  "balance": 4
}
{
  "creation_height": "567462",
  "completion_time": "2022-12-01T05:57:49.397647828Z",
  "initial_balance": "112142",
  "balance": 0.112142
}

I have been experimenting with map and select to try and get the results in a format that I am looking for, but am unsuccessful. The two things I am looking to do is, remove initial_balance and then add delegator_address to the results.

Desired result so I can easily convert to CSV and import to a spreedsheet:


{
  "delegator_address": "tori1zkzfu5tqee0p698w3g83x8a2zswgqglj4t8z3j",
  "creation_height": "546726",
  "completion_time": "2022-11-29T20:41:54.102529873Z",
  "balance": 1
}
{
  "delegator_address": "tori1rjg0vg9hlu8kygnrdehzt3z0t4ea5ssu6wkm06",
  "creation_height": "560257",
  "completion_time": "2022-11-30T18:22:32.286053755Z",
  "balance": 4
}
{
  "delegator_address": "tori1aw44evl5zlqtw93zln83tp4v3w4kp6e9zmx64k",
  "creation_height": "567462",
  "completion_time": "2022-12-01T05:57:49.397647828Z",
  "balance": 0.112142
}

CodePudding user response:

You can construct your output from scratch:

.unbonding_responses[]
| { delegator_address }
  (.entries[] | { 
    creation_height,
    completion_time,
    balance: (.balance|tonumber/1000000)
})

or merge full entries objects with delegator address and then delete the unwanted property normalize your balance:

.unbonding_responses[]
| { delegator_address }   .entries[]
| del(.initial_balance)
| .balance |= tonumber/1000000

Output for both programs:

{
  "delegator_address": "tori1zkzfu5tqee0p698w3g83x8a2zswgqglj4t8z3j",
  "creation_height": "753019",
  "completion_time": "2022-12-13T16:00:56.664864370Z",
  "balance": 3
}
{
  "delegator_address": "tori1rjg0vg9hlu8kygnrdehzt3z0t4ea5ssu6wkm06",
  "creation_height": "653923",
  "completion_time": "2022-12-07T00:53:48.247870844Z",
  "balance": 50
}
{
  "delegator_address": "tori1aw44evl5zlqtw93zln83tp4v3w4kp6e9zmx64k",
  "creation_height": "597161",
  "completion_time": "2022-12-03T05:32:59.107845572Z",
  "balance": 2
}
  • Related