【问题标题】:how to extract one filed from curl response如何从卷曲响应中提取一个字段
【发布时间】:2022-11-18 09:44:37
【问题描述】:

我想从我的 curl 响应中提取一个文件,但 jq 对我不起作用。

这是我的回应:

response = {"response":[{"id":"0-0","enabled":true}]}

我想提取响应决策(对于上述情况,决策为真,因为“启用”的值为真)。有什么建议么?谢谢。

我试过了

echo $response | grep -o '"enabled":"[^"]*' | grep -o '[^"]*$'

jq -r .enabled <<< $respons

对于 jq:找不到命令

【问题讨论】:

  • "enabled": 之后没有",而你grep 是其中之一。

标签: json bash curl


【解决方案1】:
#!/bin/bash

response="{"response":[{"id":"0-0","enabled":true}]}"

echo "$response" | jq '.response[0].enabled'

将返回 true。

在这里阅读 jq:http://www.compciv.org/recipes/cli/jq-for-parsing-json/


我如何构建 jq 字符串:

$ echo "$response" | jq '.'
{
"response": [
    {
    "id": "0-0",
    "enabled": true
    }
]
}

$ echo "$response" | jq '.response'
[
{
    "id": "0-0",
    "enabled": true
}
]

$ echo "$response" | jq '.response[0]'
{
"id": "0-0",
"enabled": true
}

$ echo "$response" | jq '.response[0].enabled'
true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多