如何使用 jq 从 json 对象的数组中的键中提取值?

How do I extract a Value from a Key in an Array from a json object using jq?

我正在编写需要进行两次调用的 bash 脚本,第二次调用需要来自第一次调用的 json 响应的值。 但是,当我尝试检索此值时出现错误。

jq: error: Could not open file ............. Invalid argument

我已经在浏览器中检查了我的 jq 查询,它工作正常。 这是我的脚本示例。

api_key="key1"
app_key="key2"

response=$(curl -X POST \
-H 'Content-Type: application/json' \
-H "DD-API-KEY: ${api_key}" \
-H "DD-APPLICATION-KEY: ${app_key}" \
-d '{
    "tests": [
        {
            "public_id": "abc-123-xyz",
            "locations": ["aws:eu-west-2"]
         
        }
    ]
}' "https://URL")

resultId=$(jq '.results[].result_id' $response)

curl -G \
    "https://URL" \
    -H "DD-API-KEY: ${api_key}" \
    -H "DD-APPLICATION-KEY: ${app_key}" \
    -d "result_ids=[$resultId]"

这是 json 响应的示例

{
    "results": [
        {
            "result_id": "1234",
            "public_id": "abc-123-xyz",
            "location": 1
        }
    ],
    "triggered_check_ids": [
        "abc-123-xyz"
    ],
    "locations": [
        {
            "is_active": boolean,
            "region": "Somewhere",
            "display_name": "A1",
            "id": 1,
            "name": "A1"
        },
        {
            "is_active": boolean,
            "region": "Somewhere else",
            "display_name": "B2",
            "id": 2,
            "name": "B2"
        }
    ]
}

shell 变量 $response 包含一个 JSON 值,而不是文件名,因此:

resultId=$(jq '.results[].result_id' <<< "$response")