[Bash] Parsing JSON with jq cheatsheet

Peter Chang
1 min readMar 21, 2019

--

As a Javascripter, how we communicate with different script is SPEAKing in JSON data, it takes me sometime to find out things is a bit different in Command line shell script. It surprise me that We need other to parse JSON (isnt just require(‘json.json’) ).

jq is a command-line tool for parsing JSON.

Let say there is a json file data.json

[{title, url}, {} {} ...]

1. To Access the url link in the json file in shell script

$ cat ./data.json | jq '.[].url'

2. Iterating the JSON Array

jq -c '.[]' ./where/the/file.json | while read i; do
title=`echo $i | jq '.title' -r`
url=`echo $i | jq '.url' -r`
echo ${title}
echo ${url}
someCommand --title=$title --url=$url
done

Sign up to discover human stories that deepen your understanding of the world.

--

--

Peter Chang
Peter Chang

No responses yet

Write a response