Curl
從 curl 返回的 JSON 文件中提取值
[ec2-user@ip-10-0-1-238 ~]$ curl "https://tools.keycdn.com/geo.json?host={18.205.6.240}" {"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.com, Inc.","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.0481,"longitude":-77.4728,"metro_code":511,"timezone":"America/New_Y
在這個輸出中,我需要提取 country_name。所以,我不太確定該怎麼做?
$ curl "https://tools.keycdn.com/geo.json?host={18.205.6.240}" 2>/dev/null | jq -r '.data.geo.country_name' United States
該
jq
表達式.data.geo.country_name
提取從您使用訪問的端點返回的 JSON 文件中的給定項目curl
。
如果您也可以使用以下版本:以下是 REST API 輸出:
{ "error": "none", "message": "Success", "data": { "name": "Vishal Biradar", "userId": 1, } }
下面是顯示我們如何閱讀它的腳本程式碼。
#!/bin/bash LOGIN_DATA=$(\ curl -d "email=abcd@gmail.com&password=abcd123" \ http://localhost:1234/login \ | python -c "import sys, json; print json.load(sys.stdin)['data']['name']"\ ) echo "LOGIN_DATA=$LOGIN_DATA"