shell/bash解析JSON
2017-11-27 17:03
197 查看
格式化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
相关文章推荐
- Shell解析Json字符串
- linux系统下解析shell标准输出流生成json包demon
- Linux操作系统基础解析之(七)——Bash(Shell)基础知识(1)
- shell 解析 json
- Zsh vs. Bash不完全对比解析,zsh是一种更强大的被成为“终极”的Shell
- shell jsonj解析
- Zsh vs. Bash不完全对比解析,zsh是一种更强大的被成为“终极”的Shell
- linux bash Shell脚本经典之Fork炸弹解析:() { :|:& };:
- Linux操作系统基础解析之(七)——Bash(Shell)基础知识(3)
- Shell里内置数组BASH_REMATCH[n]个人解析
- Linux操作系统基础解析之(七)——Bash(Shell)基础知识(4)
- Linux操作系统基础解析之(七)——Bash(Shell)基础知识(5)
- bash/shell 解析命令行参数工具:getopts/getopt
- Linux操作系统基础解析之(七)——Bash(Shell)基础知识(6)
- Linux 命令详解(十一)Shell 解析 json命令jq详解
- 几个有用的Shell,解析json,解析URL
- 用shell中用jq解析json遇到compile error
- 几个有用的Shell,解析json,解析URL
- shell解析json字符串
- Zabbix监控之Linux命令行/Shell脚本解析json