如何从HTTP GET JSON结果获取字段到文件?

时间:2016-07-01 02:02:22

标签: json bash http

我正在尝试向API服务发出HTTP GET请求,并将JSON结果中的一个返回字段推送到txt文件。

基于此前提出的问题:(Getting JSON value from cURL in Linux Bash) ...我有一个bash脚本如下......

TOKEN_FILE="/myhome/project/resources/auto_token.txt"

AUTH_RESULT=$(curl -i -H "Content-Type: application/json" "https://access.mywebservice.com/access/oauth/token?grant_type=client_credentials&client_id=123456&client_secret=MySecretPassword");

RESULT_FIELDS=$( cat <<EOF | json_reformat | \
    sed -rne '/:/s@^\s+"(\w+)":\s+"([^"]+)",?@json_\1="\2"@gp'
[$AUTH_RESULT]
EOF
)

if [ -f "$TOKEN_FILE" ]
then 
    echo "$RESULT_FIELDS" > "$TOKEN_FILE"
fi

预期的JSON结果如下所示(从Postman复制):

{
  "access_token": "eyJ5bGciOiJSUzI1NiJ6.eyJzY29wZSI6WyJDUl7iLCJNQVAiLCJQVFkiLCJ8R1QiLCJTVFMiLCJUVEwiXSwiaXNzIjoiaHR0cHM6Ly9hY2Nlc3MtdWF0LWFwaS5jb3JlbG9naWMuYXNpYSIsImVudl9hx2Nlc3NfcmVzdHJpY3QiOmZhbHNlLCJleHAiOjE0NjcyODMwODcsImNsaWVudF9pZCI6IjhhOTY4OGJjIn0.F2iQfVsi9zntOxKYrNRukSIwuQ_LGSi_WMIXKII2A3GOEaqs-WmFTi7az9rvvfDsOl9rHy_s_66A6PiCpPftyw21Fl0aZZRoFcKv2H_zDUHuxOEs8V36jHeLghV7pjHwYI_nG68CIGvfuRWFNzQuiMFWc_i8oB3n5noSd8fQqa4",
  "token_type": "bearer",
  "expires_in": 43199,
  "scope": "PROD1 PROD2 PROD3",
  "iss": "https://access.mywebservice.com",
  "env_access_restrict": false
}

我收到以下错误...

bash-4.1$ ./token_renewal_test_05.sh
: command not foundt_05.sh: line 2:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
115   576    0   576    0     0   2266      0 --:--:-- --:--:-- --:--:-- 30315
: command not foundt_05.sh: line 3:
: command not foundt_05.sh: line 4:
./token_renewal_test_05.sh: line 14: warning: here-document at line 10 delimited by end-of-file (wanted `EOF')
./token_renewal_test_05.sh: line 13: warning: here-document at line 9 delimited by end-of-file (wanted `EOF')
: command not foundt_05.sh: line 13:
lexical error: invalid char in json text.
                                       sed -rne '/:/s@^\s+"(\w+)":\s+"
                     (right here) ------^
: command not foundt_05.sh: line 10:
./token_renewal_test_05.sh: line 16: syntax error: unexpected end of file

我对bash有点新意,尽管看起来是直接指向该问题的指针但是解决了这个问题(请注意这是版本5)!

任何人都可以提供任何帮助吗?

PS:我也没有jq。

谢谢!

此致

克里斯

2 个答案:

答案 0 :(得分:0)

根据this comment Parsing JSON with UNIX tools上的{{3}}注释。

适用于您的格式的工作解决方案:

eval $(cat <<EOF | \
    sed -re 's/(,|\{|\})//g' | \
    sed -re 's/"(\w+)":\s*"?([^"]*)"?$/json_\1='\''\2'\''/'
$JSON
EOF
)
set | grep '^json_'
json_access_token=eyJ5bGciOiJSUzI1NiJ6.eyJzY29wZSI6WyJDUl7iLCJNQVAiLCJQVFkiLCJ8R1QiLCJTVFMiLCJUVEwiXSwiaXNzIjoiaHR0cHM6Ly9hY2Nlc3MtdWF0LWFwaS5jb3JlbG9naWMuYXNpYSIsImVudl9hx2Nlc3NfcmVzdHJpY3QiOmZhbHNlLCJleHAiOjE0NjcyODMwODcsImNsaWVudF9pZCI6IjhhOTY4OGJjIn0.F2iQfVsi9zntOxKYrNRukSIwuQ_LGSi_WMIXKII2A3GOEaqs-WmFTi7az9rvvfDsOl9rHy_s_66A6PiCpPftyw21Fl0aZZRoFcKv2H_zDUHuxOEs8V36jHeLghV7pjHwYI_nG68CIGvfuRWFNzQuiMFWc_i8oB3n5noSd8fQqa4
json_env_access_restrict=false
json_expires_in=43199
json_iss=https://access.mywebservice.com
json_scope='PROD1 PROD2 PROD3'
json_token_type=bearer

答案 1 :(得分:0)

再次感谢Chepner和Drew

我在Sed遇到太多问题(可能是因为我缺乏经验)。事实证明,我尝试使用lookbehind。 Sed没有这个,但grep这样做,知道我的JSON响应的strcuture永远不会有机会,我能够使用grep代替我的令牌... grep -o -P '(?<="access_token":").*(?=","token_type")'