如何在shell脚本中解析json响应? [英] How to parse json response in the shell script?
问题描述
我正在使用bash shell脚本.我需要使用shell脚本执行URL,然后解析来自其中的json数据.
I am working with bash shell script. I need to execute an URL using shell script and then parse the json data coming from it.
这是我的URL- http://localhost:8080/test_beat
,点击URL后我得到的响应将来自这两个-
This is my URL - http://localhost:8080/test_beat
and the responses I can get after hitting the URL will be from either these two -
{"error": "error_message"}
{"success": "success_message"}
下面是我的shell脚本,该脚本使用wget执行URL.
Below is my shell script which executes the URL using wget.
#!/bin/bash
DATA=$(wget -O - -q -t 1 http://localhost:8080/test_beat)
#grep $DATA for error and success key
现在,我不确定如何解析$ DATA中的json响应并查看密钥是成功还是错误.如果键是成功的,那么我将打印一条消息成功"并打印$ DATA值,并退出状态代码为零的shell脚本,但是如果键是错误的话,我将打印错误"并打印$ DATA值并以非零状态代码退出shell脚本.
Now I am not sure how to parse json response in $DATA and see whether the key is success or error. If the key is success, then I will print a message "success" and print $DATA value and exit out of the shell script with zero status code but if the key is error, then I will print "error" and print $DATA value and exit out of the shell script with non zero status code.
如何解析json响应并在shell脚本中从中提取密钥?
How can I parse json response and extract the key from it in shell script?
我不想安装任何库来执行此操作,因为我的JSON响应是固定的,并且始终与上面显示的相同,因此任何更简单的方法都可以.
I don't want to install any library to do this since my JSON response is fixed and it will always be same as shown above so any simpler way is fine.
更新:-
下面是我的最终shell脚本-
Below is my final shell script -
#!/bin/bash
DATA=$(wget -O - -q -t 1 http://localhost:8080/tester)
echo $DATA
#grep $DATA for error and success key
IFS=\" read __ KEY __ MESSAGE __ <<< "$DATA"
case "$KEY" in
success)
exit 0
;;
error)
exit 1
;;
esac
这看起来正确吗?
推荐答案
#!/bin/bash
IFS= read -d '' DATA < temp.txt ## Imitates your DATA=$(wget ...). Just replace it.
while IFS=\" read -ra LINE; do
case "${LINE[1]}" in
error)
# ERROR_MSG=${LINE[3]}
printf -v ERROR_MSG '%b' "${LINE[3]}"
;;
success)
# SUCCESS_MSG=${LINE[3]}
printf -v SUCCESS_MSG '%b' "${LINE[3]}"
;;
esac
done <<< "$DATA"
echo "$ERROR_MSG|$SUCCESS_MSG" ## Shows: error_message|success_message
* %b
在相应的参数中扩展反斜杠转义序列.
* %b
expands backslash escape sequences in the corresponding argument.
更新,因为我一开始并没有真正得到这个问题.它应该简单地是:
Update as I didn't really get the question at first. It should simply be:
IFS=\" read __ KEY __ MESSAGE __ <<< "$DATA"
[[ $KEY == success ]] ## Gives $? = 0 if true or else 1 if false.
您可以进一步检查它:
case "$KEY" in
success)
echo "Success message: $MESSAGE"
exit 0
;;
error)
echo "Error message: $MESSAGE"
exit 1
;;
esac
当然可以用它进行类似的明显测试:
Of course similar obvious tests can be done with it:
if [[ $KEY == success ]]; then
echo "It was successful."
else
echo "It wasn't."
fi
从您最后的评论中可以很容易地完成
From your last comment it can be simply done as
IFS=\" read __ KEY __ MESSAGE __ <<< "$DATA"
echo "$DATA" ## Your really need to show $DATA and not $MESSAGE right?
[[ $KEY == success ]]
exit ## Exits with code based from current $?. Not necessary if you're on the last line of the script.
这篇关于如何在shell脚本中解析json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!