Q:

Parse JSON in Bash

I'm trying to get the value of a json array but I don't manage to get the value of a leaf, can anyone help me?
Here's the json from maps.googleapis.com/maps/api
I'd like to get the duration text and value and here's the script I've been using so far.
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

curl --get --include 'http://maps.googleapis.com/maps/api/directions/json?origin=59.94473,30.294254&destination=59.80612,30.308552&sensor=false&departure_time=1346197500&mode=transit&format=json'  | jsonValue duration["value"] 2

Thanks in advance,
Jeremie.

A:

You are looking for jq
(a lightweight and flexible command-line JSON processor).

