Skip to content

↪️ Output processing

coccoinomane edited this page Jan 1, 2023 · 1 revision

Many web3cli commands yield output in JSON format. For example:

w3 block latest

will yields a JSON with the latest block:

{
    "baseFeePerGas": 12396216465,
    "gasUsed": 8885237,
    "hash": "0xf2ab4621bfd575fa0735f795023cdddc3cd8089b51b06248a1955a7499492b26",
    "number": 16311940,
    "transactions": [
        "0x878fe7ad4a0313598d2f58f17be20c2896f9b6e6df27c610e39ff10bfb1a99b6",
        "0x1d060bd681c6a49ea1d7430f1c1293a357f01ff9e064bdcd3c540792ddc13eaf",
    ],
    "...",
}

This output can be then further manipulated using the jq tool. Some examples follow, for more complex usage try the jq playgrond.

Extract a single field

w3 block latest | jq -r '.hash'
w3 block latest | jq -r '.baseFeePerGas'
w3 block latest | jq -r '.gasUsed'
w3 block latest | jq -r '.transactions'

Extract array elements

First and last transaction:

w3 block latest | jq -r '.transactions[1]'
w3 block latest | jq -r '.transactions[-1]'

First and last 3 transactions:

w3 block latest | jq -r '.transactions[:3]'
w3 block latest | jq -r '.transactions[-3:]'