如何在 Bash 中解析对 Json 的 CURL 响应

问题描述 投票:0回答:1

我正在尝试获得卷曲响应并能够像对象一样调用它。我已经在谷歌上搜索了一段时间,看到了很多关于 jq 的答案。我一直在努力使其工作,但我似乎找不到确切的语法。我理想的解决方案是获取所有 uploaded_at 值,然后排序,然后确定哪个是最旧的

这是我的curl命令:

curl -u "xxx:xxx" \
-X GET "https://api-cloud.browserstack.com/app-automate/recent_apps"

它返回类似:

[{"app_name":"FlyreelSample.ipa","app_version":"1.1.0","app_url":"bs://76867867","app_id":"76867867","uploaded_at":"2024-08-21 14:41:53 UTC"},{"app_name":"FlyreelSample.ipa","app_version":"1.1.0","app_url":"bs://8567463","app_id":"8567463","uploaded_at":"2024-08-21 14:40:29 UTC"},{"app_name":"app-stage-debug.apk","app_version":"1.0","app_url":"bs://834534534","app_id":"834534534","uploaded_at":"2024-08-12 15:00:34 UTC"}]

我尝试过以下命令:

curl -u "xxxx:xxxxx" \
-X GET "https://api-cloud.browserstack.com/app-automate/recent_apps" | jq -r '.uploaded_at'

我收到以下错误:

jq: error (at <stdin>:0): Cannot index array with string "uploaded_at"

我还尝试了一些 sed 和 awk 解决方案。也尝试将其放入文件但也不起作用。它永远不会将值放入文件中

curl -u "xxxx:xxxxx" \
-X GET "https://api-cloud.browserstack.com/app-automate/recent_apps" -d @test.txt
bash curl
1个回答
0
投票

我知道有很多评论都有精彩的片段,但为了方便其他读者,我想回答这个问题。

要求抓取所有uploaded_at值,然后排序,然后确定哪个是最旧的

要获取最早的日期:

curl -s https://api-cloud.browserstack.com/app-automate/recent_apps | jq -r 'sort_by(.uploaded_at)[0].uploaded_at'

获取最早的uploaded_at的相关对象:

curl -s https://api-cloud.browserstack.com/app-automate/recent_apps | jq -r 'sort_by(.uploaded_at)[0]'
© www.soinside.com 2019 - 2024. All rights reserved.