Centos安装 jq命令处理json数据
sudo yum install -y epel-release
yum list jq
yum install jq
jq合并多个json文件
file1
{
"value1": 200,
"timestamp": 1382461861,
"value": {
"aaa": {
"value1": "v1",
"value2": "v2"
},
"bbb": {
"value1": "v1",
"value2": "v2"
},
"ccc": {
"value1": "v1",
"value2": "v2"
}
}
}
file2
{
"status": 200,
"timestamp": 1382461861,
"value": {
"aaa": {
"value3": "v3",
"value4": 4
},
"bbb": {
"value3": "v3"
},
"ddd": {
"value3": "v3",
"value4": 4
}
}
}
合并
jq -s '.[0] * .[1]' file1 file2
jq -s '.[0].value * .[1].value | {value: .}' file1 file2
合并后
{
"value1": 200,
"timestamp": 1382461861,
"value": {
"aaa": {
"value1": "v1",
"value2": "v2",
"value3": "v3",
"value4": 4
},
"bbb": {
"value1": "v1",
"value2": "v2",
"value3": "v3"
},
"ccc": {
"value1": "v1",
"value2": "v2"
},
"ddd": {
"value3": "v3",
"value4": 4
}
},
"status": 200
}
jq合并两个json文本内容
echo '{"a":"foo","b":"bar"} {"c":"baz","a":0}' | jq -s add
{
"a": 0,
"b": "bar",
"c": "baz"
}
echo '{"A": {"a": 1}}' '{"A": {"b": 2}}' '{"B": 3}' |\
jq --slurp 'reduce .[] as $item ({}; . * $item)'
{
"A": {
"a": 1,
"b": 2
},
"B": 3
}
更多请参考 https://stackoverflow.com/questions/19529688/how-to-merge-2-json-objects-from-2-files-using-jq
jq去除解析内容的双引号
jq 带上-r 选项可以去掉双引号 例如
echo '{"a":"foo","b":"bar"} | jq .a
"foo"
#带-r选项
echo '{"a":"foo","b":"bar"} | jq -r .a
foo
更多jq用法
更多jq命令使用方法可以执行man jq 查看帮助文档
参考
1. How to merge 2 JSON objects from 2 files using jq? 2. 如何删除jq输出中用于解析bash中的json文件的双引号?
|