AVA DENALI Tips #2- How to get more beautiful and readable outputs for API calls using “jq” ?

Published by AVAX on

AVAX DENALI Tips #2- How to get more beautiful and readable outputs for API calls using “jq” ?

Hello Avalidators !

Clients interact with Avalanche through APIs calls to nodes.

You will be using Avalanche API calls via curl command as shown in AVA document here.

However, you may experience that Avalanche API calls via curl outputs may not be pretty as shown in documentation.

So, how can we get more beautiful and readable outputs for API calls ?

A good answer may be using “jq”.

jq is a lightweight and flexible command-line JSON processor. (jq link)

First we need to install “jq” with following command on ubuntu linux.

sudo apt-get install jq

Now, we have “jq”, we can test and see how it makes a difference.

Lets see the difference by testing “admin.getNodeID” API call via curl before and after using “jq”.

BEFORE : without “jq”

curl -s -X POST --data '{
"jsonrpc": "2.0",
"method": "admin.getNodeID",
"params":{},
"id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/admin

AFTER : with “jq”

curl -s -X POST --data '{
"jsonrpc": "2.0",
"method": "admin.getNodeID",
"params":{},
"id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/admin | jq

“jq” is a very practical tool that may help formatting API call outputs.

Let’s see another sample.

Is “NODE ID” listed as a validator or not ?

Lets see the difference by testing “platform.getCurrentValidators” API call via curl before and after using “jq”.

We can use “jq” to check if “NODE ID” is listed as a validator or not ?
if “NODE ID” is listed as a validator then It prints “NODE ID” as output
else it does not print “NODE ID” as output

BEFORE : without “jq”

curl -X POST --data '{
"jsonrpc": "2.0",
"method": "platform.getCurrentValidators",
"params": {},
"id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/P

AFTER : with “jq”

curl -X POST --data '{
"jsonrpc": "2.0",
"method": "platform.getCurrentValidators",
"params": {},
"id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/P | jq '.result.validators[] | select(.id == "YOUR NODE ID ") | .id'

I hope you will find “jq” as a practical tool.

Have fun! And let everyone know in Discord channel if you need assistance.


AVA DENALI Tips #2- How to get more beautiful and readable outputs for API calls using “jq” ? was originally published in Avalanche Hub on Medium, where people are continuing the conversation by highlighting and responding to this story.

Categories: AvaxNews