Curl 与jq 发起json请求

Posted 朝花夕拾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Curl 与jq 发起json请求相关的知识,希望对你有一定的参考价值。

1. 安装

官网:https://stedolan.github.io/jq/download/
基本就是brew install、apt install、yum install之类的,很简单

2. 常用操作

2.1 格式化json

直接请求,格式明显不易读

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc
{"_links":{"next":{"href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=desc"},"prev":{"href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=asc"}},"_embedded":{"records":[{"memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=","id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e","paging_token":"120624397026840576","successful":true,"hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e","ledger":28085056,"created_at":"2020-02-05T13:01:24Z","source_account_sequence":"112094093760491433","fee_paid":100,"fee_charged":100,"max_fee":100,"operation_count":1,"result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=","memo_type":"hash","valid_after":"1970-01-01T00:00:00Z"}]}}

使用jq格式化

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq 
{
    "_links":{
        "next":{
            "href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=desc"
        },
        "prev":{
            "href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=asc"
        }
    },
    "_embedded":{
        "records":[
            {
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
            }
        ]
    }
}

2.2 取指定字段的值

直接输入字段,使用.嵌套访问,例如要获取records

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq ._embedded.records
[
            {
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
            }
]

2.3 过滤指定字段

使用一个json指定所有字段,如{memo, hash},获取昨天的最高温度如下

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq ._embedded.records | jq ".[0]" | jq "{memo, hash}"
{
  "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
  "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e"
}

2.4 获取多个字段的值

使用逗号获取多个

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq "._embedded.records[0]" | jq ".memo, .hash"

"AaUJ0NXa2UE1j4QiOfgy5qqhD8oT8kWI9Cy4xfvb/u8="
"fc3e37d08c99496468f03adbb01f0c32b614f2423508489456c5a32157622036"

2.5 筛选数组

直接指定数组的索引即可

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc| jq "._embedded.records" | jq ".[0, 9]"
{
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
}
...

3. 参考

以上是关于Curl 与jq 发起json请求的主要内容,如果未能解决你的问题,请参考以下文章

用curl发起https请求

postman 自动生成 curl 代码片段

postman 自动生成 curl 代码片段

jq --- shell命令模式下JSON格式化利器

5.linux进价命令curl和jq

44.curl发布带body的post请求