在不使用 jq 或其他工具的情况下仅使用 bash 解析来自 curl 的 json 响应

Parse json response from curl using only bash without using jq or other tools

我有一个 curl 命令,它 return 是一个 json 响应

{
"customer_id": "9a081c0c",
"resources": ["credit-98797", "credit-98798", "loan-876", "loan-889-approved","loan-882-rejected", "loan-879-pending"],
"customer_info": "John",
"warnings": null
}

我正在尝试创建一个脚本,它将 return 最新的贷款 ID,在本例中为 loan-889-approved 但是很难做到正确。

我尝试对所有贷款进行排序并获取数组的第一个元素,但我做错了

details=$(curl -u username:token https://1.2.3.4:8420/v1/customer/details/9a081c0c)
sortedLoans=$(for x in ${details[@]}; do if [["$x"=="loan"]]; then echo $x; fi done | sort ) 

主要的挑战是仅使用 bash,不使用 jq 或 json,如果可能的话

我是 shell 脚本的新手。任何建议表示赞赏。提前致谢

既然你提到你不能使用 jq,请用 grep+sort+tail

试试这个解决方案
curl '..' | grep -o 'loan-[^"]*' | sort -t- -k2n | tail -n1

sort -t- -k2n 将根据 -

之后的数字进行排序