在shell脚本中有时候需要解析json字段信息,但是一般简单的sed&awk都存在问题,比如多层json结构解析失败。本文介绍两种比较优秀的方法,各有优缺点
使用awk解析
方法简要说明:
- 是先查找一个字符串:带双引号的key。如果没找到,则直接返回defaultValue。
- 查找最近的冒号,找到后认为值的部分开始了,直到在层数上等于0时找到这3个字符:,}]。
- 如果有多个同名key,则依次全部打印(不论层级,只按出现顺序)
- getJsonValuesByAwk方法
### 方法简要说明:
### 1. 是先查找一个字符串:带双引号的key。如果没找到,则直接返回defaultValue。
### 2. 查找最近的冒号,找到后认为值的部分开始了,直到在层数上等于0时找到这3个字符:,}]。
### 3. 如果有多个同名key,则依次全部打印(不论层级,只按出现顺序)
### @author lux feary
### 3 params: json, key, defaultValue
function getJsonValuesByAwk() {
awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
foundKeyCount = 0
while (length(json) > 0) {
# pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误
pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}
++foundKeyCount;
start = 0; stop = 0; layer = 0;
for (i = pos + length(key) + 1; i <= length(json); ++i) {
lastChar = substr(json, i - 1, 1)
currChar = substr(json, i, 1)
if (start <= 0) {
if (lastChar == ":") {
start = currChar == " " ? i + 1: i;
if (currChar == "{" || currChar == "[") {
layer = 1;
} else {
if (currChar == "{" || currChar == "[") {
++layer;
if (currChar == "}" || currChar == "]") {
--layer;
if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
stop = currChar == "," ? i : i + 1 + layer;
break;
if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
if (foundKeyCount == 0) {print defaultValue;} exit 0;
} else {
print substr(json, start, stop - start);
json = substr(json, stop + 1, length(json) - stop)
}
- 测试脚本
json='{"code":200,"msg":"success","data":{"orderNo":"test_order_no"}}'
getJsonValuesByAwk "$json" "code" "defaultValue"
getJsonValuesByAwk "$json" "data" "defaultValue"
getJsonValuesByAwk "$json" "orderNo" "defaultValue"
- 结果反馈
200
{"orderNo":"test_order_no"}
"test_order_no"
使用专门的Json解析库: jq - Command-line JSON processor
- 安装
# 安装
yum install jq
- 格式化展示
echo '{"name":"tenmao","age":10,"hobbies":["bar","foo"],"address":{"province":"gd","city":"sz"}}' | jq
JSON格式化
-
提取特定的值:
use_jq.sh
raw='{"name":"tenmao","age":10,"hobbies":["bar","foo"],"address":{"province":"gd","city":"sz"}}'
echo $raw | jq -r '.name'
echo $raw | jq -r '.hobbies'
echo $raw | jq -r '.hobbies[0]'
echo $raw | jq -r '.address.city'
[root@VM_77_51_centos ~]# bash use_jq.sh
tenmao