【bash】parse json by jq

parse json by jq

source from

install on mac

  • mac : brew install jq

install on linux

  • linux(32bit) : wget http://stedolan.github.io/jq/download/linux32/jq
  • linux(64bit) : wget http://stedolan.github.io/jq/download/linux64/jq
  • chmod +x ./jq
  • sudo cp jq /usr/bin

example file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
        "name": "Google",
        "location":
                {
                        "street": "1600 Amphitheatre Parkway",
                        "city": "Mountain View",
                        "state": "California",
                        "country": "US"
                },
        "employees":
                [
                        {
                                "name": "Michael",
                                "division": "Engineering"
                        },
                        {
                                "name": "Laura",
                                "division": "HR"
                        },
                        {
                                "name": "Elise",
                                "division": "Marketing"
                        }
                ]
}

use

  • To parse a JSON object: cat json.txt | jq '.name'
    • output : “Google”
  • To parse a nested JSON object: cat json.txt | jq '.location.city'
    • output : “Mountain View”
  • To parse a JSON array: cat json.txt | jq '.employees[0].name'
    • output : “Michael”
  • To extract specific fields from a JSON object: cat json.txt | jq '.location | {street, city}'
    • output :
      1
      2
      3
      4
      {
      "city": "Mountain View",
      "street": "1600 Amphitheatre Parkway"
      }