Shell简单解析JSON

https://blog.csdn.net/zzhouqianq/article/details/78646861

使用awk解析JSON

使用

json='{"country":"China", "address":{"province":"guangdong","city":"shenzhen","numberOfPeople":17682000}, "numberOfPeople":"One billion four hundred million"}'

country=`getJsonValuesByAwk "$json" "country" "default"`
address=`getJsonValuesByAwk "$json" "address" ""`
province=`getJsonValuesByAwk "$address" "province" ""`
city=`getJsonValuesByAwk "$address" "city" ""`
numberOfPeople=`getJsonValuesByAwk "$json" "numberOfPeople" "a lot of"`

echo $country #"China"
echo $address #{"province":"guangdong","city":"shenzhen","numberOfPeople":17682000}
echo $province #"guangdong"
echo $city #"shenzhen"
echo $numberOfPeople #17682000 "One billion four hundred million"

原方法如下

### 方法简要说明:
### 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)
        }
    }'
}

使用第三方jq

官网:https://stedolan.github.io/jq/
参考:https://cloud.tencent.com/developer/article/1393873
参考:https://www.jianshu.com/p/6de3cfdbdb0e

brew install jq

你可能感兴趣的:(Shell简单解析JSON)