阅读shell脚本JSON数据
问题描述:
在外壳我有一个要求,其中我要读JSON响应这是在下面的格式 -
In shell I have a requirement wherein I have to read the json response which is in the following format -
{ "Messages": [ { "Body": "172.16.1.42|/home/480/1234/5-12-2013/1234.toSort", "ReceiptHandle": "uUk89DYFzt1VAHtMW2iz0VSiDcGHY+H6WtTgcTSgBiFbpFUg5lythf+wQdWluzCoBziie8BiS2GFQVoRjQQfOx3R5jUASxDz7SmoCI5bNPJkWqU8ola+OYBIYNuCP1fYweKl1BOFUF+o2g7xLSIEkrdvLDAhYvHzfPb4QNgOSuN1JGG1GcZehvW3Q/9jq3vjYVIFz3Ho7blCUuWYhGFrpsBn5HWoRYE5VF5Bxc/zO6dPT0n4wRAd3hUEqF3WWeTMlWyTJp1KoMyX7Z8IXH4hKURGjdBQ0PwlSDF2cBYkBUA=", "MD5OfBody": "53e90dc3fa8afa3452c671080569642e", "MessageId": "e93e9238-f9f8-4bf4-bf5b-9a0cae8a0ebc" } ] }
在这里,我只关心身体的属性值。我做了一些不成功的尝试像
Here I am only concerned with the "Body" property value. I made some unsuccessful attempts like
jsawk -a 'return this.Body'
或
awk -v k="Body" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}
但是这并没有足够的。谁能帮我这个?
But that did not suffice. Can anyone help me with this?
答
我尝试以下和它的工作。
I tried the following and it worked.
grep -Po '(?<="Body": ")[^"]*'
即使命令行处理器JSON JQ称为将是一个很大的帮助。随着JQ它会像这样简单 -
Even command-line JSON processor called jq would be a great help. With jq it will be as simple as this -
jq '.Body'
请访问此为JQ - http://xmodulo.com/2013/05/how-to-parse-json-string-via-command-line-on-linux.html