user1305398 user1305398 - 1 year ago 65
Bash Question

Read the json data in shell script

In shell I have a requirement wherein I have to read the json response which is in the following format -

{ "Messages": [ { "Body": "|/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?

Answer Source

I tried the following and it worked.

  grep -Po '(?<="Body": ")[^"]*'

Even command-line JSON processor called jq would be a great help. With jq it will be as simple as this -

 jq '.Body'

Visit this for jq -