bash 在shell脚本中解析json数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39227191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse json array in shell script
提问by Abdul Manaf
i need to print key and values from a json string. i allready parse a simple json string
我需要从 json 字符串打印键和值。我已经解析了一个简单的 json 字符串
{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1",
}
my code like this
我的代码是这样的
q1=$(echo $x | grep -Po '"Name":.*?[^\]",'| perl -pe 's/"Name": //; s/^"//; s/",$//');
q2=$(echo $x | grep -Po '"Id":.*?[^\]",'| perl -pe 's/"Id": //; s/^"//; s/",$//');
echo $q1 "," $q2;
But this code is not applicable for json string like this
但是这段代码不适用于这样的 json 字符串
x='{ "TestNames":
[{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1"
},
{
"Name": "test2",
"CreateDate": "2016-08-30T10:52:13Z",
"Id": "testId2"
}]
}';
I need to print like this
我需要像这样打印
test1 , testId1
test2 , testId2
is it possible to get data like this using grep command?
是否可以使用 grep 命令获取这样的数据?
回答by hek2mgl
First, your data is not valid json, there is a comma too much:
首先,你的数据不是有效的json,逗号太多了:
{
"TestNames": [
{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1", <--- Remove that!
},
{
"Name": "test2",
"CreateDate": "2016-08-30T10:52:13Z",
"Id": "testId2"
}
]
}
Once you've fixed that you can use jq
for parsing json on the command line:
修复后,您可以jq
在命令行上使用解析 json:
echo "$x" | jq -r '.TestNames[]|"\(.Name) , \(.Id)"'
if you need to keep the output values.
如果您需要保留输出值。
declare -A map1
while read name id ; do
echo "$name"
echo "$id"
map1[$name]=$id
done < <(echo "$x" | jq -r '.TestNames[]|"\(.Name) \(.Id)"')
echo "count : ${#map1[@]}"
echo "in loop: ${map1[$name]}"
回答by Aaron
I'd recommend using jq
, a command-line JSON
parser :
我建议使用jq
命令行JSON
解析器:
$ echo '''{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1"
}''' | jq '.Name + " , " + .Id'
"test1 , testId1"
$ echo '''{ "TestNames":
[{
"Name": "test1",
"CreateDate": "2016-08-30T10:52:52Z",
"Id": "testId1"
},
{
"Name": "test2",
"CreateDate": "2016-08-30T10:52:13Z",
"Id": "testId2"
}]
}''' | jq '.TestNames[] | .Name + " , " + .Id'
"test1 , testId1"
"test2 , testId2"