命令
curl "https://tools.keycdn.com/geo.json?host={18.205.6.240}"
返回以下 JSON 文档:
{"status":"success","description":"Data successfully received.","data":{"geo":{"host":"18.205.6.240","ip":"18.205.6.240","rdns":"ec2-18-205-6-240.compute-1.amazonaws.com","asn":14618,"isp":"AMAZON-AES","country_name":"United States","country_code":"US","region_name":"Virginia","region_code":"VA","city":"Ashburn","postal_code":"20149","continent_name":"North America","continent_code":"NA","latitude":39.0469,"longitude":-77.4903,"metro_code":511,"timezone":"America\/New_York","datetime":"2022-06-17 10:44:39"}}}
在此输出中,我需要提取country_name
.我不知道该怎么做。
答案1
$ curl -s 'https://tools.keycdn.com/geo.json?host={18.205.6.240}' | jq -r '.data.geo.country_name'
United States
该jq
表达式.data.geo.country_name
提取从您使用 访问的端点返回的 JSON 文档中的给定项目curl
。
答案2
如果您也可以使用以下版本:下面是 REST API 输出:
{
"error": "none",
"message": "Success",
"data": {
"name": "Vishal Biradar",
"userId": 1,
}
}
下面是显示我们如何读取它的脚本代码。
#!/bin/bash
LOGIN_DATA=$(\
curl -d "[email protected]&password=abcd123" \
http://localhost:1234/login \
| python -c "import sys, json; print json.load(sys.stdin)['data']['name']"\
)
echo "LOGIN_DATA=$LOGIN_DATA"