shell/bash解析JSON

格式化JSON

### Example usage: echo "$json" | formatJson
### @author lux feary
function formatJson() {
    # python -m json.tool
    python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin), sort_keys=True, indent=4));sys.stdout.write('\n');"
}

使用node解析JSON

### Use node to parse json if installed, support subkeys
### Example json: {"a": 1, "b": {"c": "zhouqian"}}
### Example usage: getJsonValueByNode "$json" "b.c"
### @author lux feary
###
### 2 params: json, key
function getJsonValueByNode() {
    if which node; then
        local json="$1"
        local key="$2"
        # node -pe "JSON.stringify(eval($json).$key)" | awk '{print ($0 == "undefined" ? "" : $0)}'
        node -pe "JSON.stringify(JSON.parse(process.argv[1]).$key)" "$json" | awk '{print ($0 == "undefined" ? "null" : $0)}'
        return 0
    else
        return 1
    fi
}

test/example code:

json='{"a": 1, "b": {"c": "zhouqian"}}'
getJsonValueByNode "$json" "a"
getJsonValueByNode "$json" "b.c"
getJsonValueByNode "$json" "b"
getJsonValueByNode "$json" "c"     # print null
getJsonValueByNode "$json" "c.b"   # print error message

使用Python解析JSON

### Use python to parse if installed, both python2 and python3 is fine
### Example json: {"a": 1, "b": {"c": "zhouqian"}}
### Example usage: echo "$json" | getJsonValueByPython "b" | getJsonValueByPython "c"
### @author lux feary
###
### stdin: json
### 1 params: key
function getJsonValueByPython() {
    if which python; then
        local key="$1"
        python -c "import json,sys; sys.stdout.write(json.dumps(json.load(sys.stdin).get('$key')));sys.stdout.write('\n');"
        return 0
    else
        return 1
    fi
}

test/example code:

echo "$json" | getJsonValueByPython "a"
echo "$json" | getJsonValueByPython "b" | getJsonValueByPython "c"
echo "$json" | getJsonValueByPython "b"
echo "$json" | getJsonValueByPython "c"                                # print null
echo "$json" | getJsonValueByPython "c" | getJsonValueByPython "b" # print error message

使用awk解析JSON

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

test/example code:

json='{"a": 1, "b": {"ca": "zhouqian"}, "zq": 100, "zq": "101"}'

getJsonValuesByAwk "$json" "a" "defaultValue"
getJsonValuesByAwk "$json" "b" "defaultValue"
getJsonValuesByAwk "$json" "ca" "defaultValue"
getJsonValuesByAwk "$json" "zhouqian" "defaultValue"   // print defaultValue
getJsonValuesByAwk "$json" "zq" "defaultValue"     // print 100 "101" in two lines
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值