Skip to main content

Useful jq filters

'jq is like sed for JSON data - you can use it to slice, filter, map, and transform structured data with the same ease that sed, awk, grep and friends let you play with text.'

Install jq.

Below you will find some useful jq filters to parse the governance state.

Show governance actions that will expire at the end of the current epoch:

current_epoch=$(cardano-cli query tip --testnet-magic 4 | jq .epoch)
cardano-cli conway query gov-state --testnet-magic 4 \
| jq --argjson epoch "$current_epoch" '.proposals
| to_entries[]
| select(.value.expiresAfter == $epoch)'

Show governance actions that were proposed in the current epoch:

current_epoch=$(cardano-cli query tip --testnet-magic 4 | jq .epoch)
cardano-cli conway query gov-state --testnet-magic 4 \
| jq -r --argjson epoch "$current_epoch" '.proposals
| to_entries[]
| select(.value.proposedIn == $epoch)'

Sort governance actions by the number of DRep votes:

cardano-cli conway query gov-state --testnet-magic 4 | jq -r '
.proposals
| to_entries[]
| {govActionId: .key, type: .value.action.tag, drepVoteCount: (.value.dRepVotes | keys | length)}
' | jq -s 'sort_by(.voteCount) | reverse[]'

Sort by the number of SPO votes:

cardano-cli conway query gov-state --testnet-magic 4 | jq -r '
.proposals
| to_entries[]
| {govActionId: .key, type: .value.action.tag, spoVoteCount: (.value.stakePoolVotes | keys | length)}
' | jq -s 'sort_by(.voteCount) | reverse[]'

Sort by the number of CC votes:

cardano-cli conway query gov-state --testnet-magic 4 | jq -r '
.proposals
| to_entries[]
| {govActionId: .key, ccVoteCount: (.value.committeeVotes | keys | length)}
' | jq -s 'sort_by(.voteCount) | reverse[]'

Filter actions that expire within the current and the next two epochs, including information about all roles' votes, sorted by expiration epoch:

current_epoch=$(cardano-cli query tip --testnet-magic 4 | jq .epoch)
cardano-cli conway query gov-state --testnet-magic 4 | jq -r --argjson current_epoch "$current_epoch" '
.proposals
| to_entries[]
| select(.value.expiresAfter >= ($current_epoch | tonumber) and .value.expiresAfter <= ($current_epoch + 2))
| {
govActionId: .key,
type: .value.action.tag,
expiresAfter: .value.expiresAfter,
committeeVotesCount: (.value.committeeVotes | length),
dRepVotesCount: (.value.dRepVotes | length),
stakePoolVotesCount: (.value.stakePoolVotes | length)
}
' | jq -s 'sort_by(.expiresAfter)'

Show actions for which a specific DRep key has voted, indicating the DRep's vote and the total number of votes received for this action:

Replace 058b60ead63f667c0ff5b40e269dd1f05ce3a804256735ad4eddce20 with the hex DRep ID of your interest.

cardano-cli conway query gov-state --testnet-magic 4 | jq -r --arg dRepKey "keyHash-058b60ead63f667c0ff5b40e269dd1f05ce3a804256735ad4eddce20" '
.proposals
| to_entries[]
| select(.value.dRepVotes[$dRepKey] != null)
| {
govActionId: .key,
type: .value.action.tag,
dRepVote: .value.dRepVotes[$dRepKey],
expiresAfter: .value.expiresAfter,
committeeVotesCount: (.value.committeeVotes | length),
dRepVotesCount: (.value.dRepVotes | length),
stakePoolVotesCount: (.value.stakePoolVotes | length)
}
'

Show actions where the given DRep key has not voted yet:

Replace 058b60ead63f667c0ff5b40e269dd1f05ce3a804256735ad4eddce20 with the hex DRep ID of your interest.

cardano-cli conway query gov-state --testnet-magic 4 | jq -r --arg dRepKey "keyHash-058b60ead63f667c0ff5b40e269dd1f05ce3a804256735ad4eddce20" '
.proposals
| to_entries[]
| select(.value.dRepVotes[$dRepKey] == null)
| {
govActionId: .key,
type: .value.action.tag,
expiresAfter: .value.expiresAfter,
committeeVotesCount: (.value.committeeVotes | length),
dRepVotesCount: (.value.dRepVotes | length),
stakePoolVotesCount: (.value.stakePoolVotes | length)
}
'

Show the total number of 'yes', 'no', and 'abstain' votes for a given governance action ID:

Replace cf2dce795cef4f8e92f0ab062a5dae0c1f7d8891d943ced9c2eeda9a62d8f092#0 with the governance action ID of your interest.

cardano-cli conway query gov-state --testnet-magic 4 | jq -r --arg actionId "cf2dce795cef4f8e92f0ab062a5dae0c1f7d8891d943ced9c2eeda9a62d8f092#0" '
.proposals
| to_entries[]
| select(.key == $actionId)
| { govActionId: .key,
dRepVoteYesCount: (.value.dRepVotes | with_entries(select(.value == "VoteYes")) | length),
dRepVoteNoCount: (.value.dRepVotes | with_entries(select(.value == "VoteNo")) | length),
dRepAbstainCount: (.value.dRepVotes | with_entries(select(.value == "Abstain")) | length),
stakePoolVoteYesCount: (.value.stakePoolVotes | with_entries(select(.value == "VoteYes")) | length),
stakePoolVoteNoCount: (.value.stakePoolVotes | with_entries(select(.value == "VoteNo")) | length),
stakePoolAbstainCount: (.value.stakePoolVotes | with_entries(select(.value == "Abstain")) | length),
committeeVoteYesCount: (.value.committeeVotes | with_entries(select(.value == "VoteYes")) | length),
committeeVoteNoCount: (.value.committeeVotes | with_entries(select(.value == "VoteNo")) | length),
committeeAbstainCount: (.value.committeeVotes | with_entries(select(.value == "Abstain")) | length)
}
'

Show the active treasury withdrawal governance actions and their current vote count:

current_epoch=$(cardano-cli query tip --testnet-magic 4 | jq .epoch)

cardano-cli conway query gov-state --testnet-magic 4 | jq -r --arg currentEpoch "$current_epoch" '
.proposals
| to_entries[]
| select(.value.expiresAfter > ($currentEpoch | tonumber) and .value.action.tag == "TreasuryWithdrawals")
| { govActionId: .key,
type: .value.action.tag,
expiresAfter: .value.expiresAfter,
dRepVoteYesCount: (.value.dRepVotes | with_entries(select(.value == "VoteYes")) | length),
dRepVoteNoCount: (.value.dRepVotes | with_entries(select(.value == "VoteNo")) | length),
dRepAbstainCount: (.value.dRepVotes | with_entries(select(.value == "Abstain")) | length),
committeeVoteYesCount: (.value.committeeVotes | with_entries(select(.value == "VoteYes")) | length),
committeeVoteNoCount: (.value.committeeVotes | with_entries(select(.value == "VoteNo")) | length),
committeeAbstainCount: (.value.committeeVotes | with_entries(select(.value == "Abstain")) | length)
}
' | jq -s 'sort_by(.expiresAfter)'

Show the active update committee governance actions and their current vote count:

current_epoch=$(cardano-cli query tip --testnet-magic 4 | jq .epoch)

cardano-cli conway query gov-state --testnet-magic 4 | jq -r --arg currentEpoch "$current_epoch" '
.proposals
| to_entries[]
| select(.value.expiresAfter > ($currentEpoch | tonumber) and .value.action.tag == "UpdateCommittee")
| { govActionId: .key,
type: .value.action.tag,
expiresAfter: .value.expiresAfter,
dRepVoteYesCount: (.value.dRepVotes | with_entries(select(.value == "VoteYes")) | length),
dRepVoteNoCount: (.value.dRepVotes | with_entries(select(.value == "VoteNo")) | length),
dRepAbstainCount: (.value.dRepVotes | with_entries(select(.value == "Abstain")) | length),
stakePoolVoteYesCount: (.value.stakePoolVotes | with_entries(select(.value == "VoteYes")) | length),
stakePoolVoteNoCount: (.value.stakePoolVotes | with_entries(select(.value == "VoteNo")) | length),
stakePoolAbstainCount: (.value.stakePoolVotes | with_entries(select(.value == "Abstain")) | length)
}
' | jq -s 'sort_by(.expiresAfter)'