Linux: how to parse a JSON file by command line with jq


Example, we have a JSON file:

http://dummy.restapiexample.com/api/v1/employees

{
"status":"success",
"data":[
{
"id":"1",
"employee_name":"Tiger Nixon",
"employee_salary":"320800",
"employee_age":"61",
"profile_image":""
},
{
"id":"2",
"employee_name":"Garrett Winters",
"employee_salary":"170750",
"employee_age":"63",
"profile_image":""
},
{
"id":"3",
"employee_name":"Ashton Cox",
"employee_salary":"86000",
"employee_age":"66",
"profile_image":""
},
{
"id":"4",
"employee_name":"Cedric Kelly",
"employee_salary":"433060",
"employee_age":"22",
"profile_image":""
},
{
"id":"5",
"employee_name":"Airi Satou",
"employee_salary":"162700",
"employee_age":"33",
"profile_image":""
},
{
"id":"6",
"employee_name":"Brielle Williamson",
"employee_salary":"372000",
"employee_age":"61",
"profile_image":""
},
{
"id":"7",
"employee_name":"Herrod Chandler",
"employee_salary":"137500",
"employee_age":"59",
"profile_image":""
},
{
"id":"8",
"employee_name":"Rhona Davidson",
"employee_salary":"327900",
"employee_age":"55",
"profile_image":""
},
{
"id":"9",
"employee_name":"Colleen Hurst",
"employee_salary":"205500",
"employee_age":"39",
"profile_image":""
},
{
"id":"10",
"employee_name":"Sonya Frost",
"employee_salary":"103600",
"employee_age":"23",
"profile_image":""
},
{
"id":"11",
"employee_name":"Jena Gaines",
"employee_salary":"90560",
"employee_age":"30",
"profile_image":""
},
{
"id":"12",
"employee_name":"Quinn Flynn",
"employee_salary":"342000",
"employee_age":"22",
"profile_image":""
},
{
"id":"13",
"employee_name":"Charde Marshall",
"employee_salary":"470600",
"employee_age":"36",
"profile_image":""
},
{
"id":"14",
"employee_name":"Haley Kennedy",
"employee_salary":"313500",
"employee_age":"43",
"profile_image":""
},
{
"id":"15",
"employee_name":"Tatyana Fitzpatrick",
"employee_salary":"385750",
"employee_age":"19",
"profile_image":""
},
{
"id":"16",
"employee_name":"Michael Silva",
"employee_salary":"198500",
"employee_age":"66",
"profile_image":""
},
{
"id":"17",
"employee_name":"Paul Byrd",
"employee_salary":"725000",
"employee_age":"64",
"profile_image":""
},
{
"id":"18",
"employee_name":"Gloria Little",
"employee_salary":"237500",
"employee_age":"59",
"profile_image":""
},
{
"id":"19",
"employee_name":"Bradley Greer",
"employee_salary":"132000",
"employee_age":"41",
"profile_image":""
},
{
"id":"20",
"employee_name":"Dai Rios",
"employee_salary":"217500",
"employee_age":"35",
"profile_image":""
},
{
"id":"21",
"employee_name":"Jenette Caldwell",
"employee_salary":"345000",
"employee_age":"30",
"profile_image":""
},
{
"id":"22",
"employee_name":"Yuri Berry",
"employee_salary":"675000",
"employee_age":"40",
"profile_image":""
},
{
"id":"23",
"employee_name":"Caesar Vance",
"employee_salary":"106450",
"employee_age":"21",
"profile_image":""
},
{
"id":"24",
"employee_name":"Doris Wilder",
"employee_salary":"85600",
"employee_age":"23",
"profile_image":""
}
]
}

We save to: http://jsonstore.top/?13tfpim0

curl -s http://dummy.restapiexample.com/api/v1/employees

jq is a lightweight and flexible command-line JSON processor.

Home page: https://stedolan.github.io/jq/

jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

Install jq on CentOS

yum install jq -y

Result:

[root@tutorialspots ~]# yum install jq -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     |  32 kB     00:00
 * base: mirror.alpix.eu
 * epel: mirror.imt-systems.com
 * extras: linux.darkpenguin.net
 * updates: linux.darkpenguin.net
base                                                     | 3.6 kB     00:00
extras                                                   | 2.9 kB     00:00
ius                                                      | 1.3 kB     00:00
nodesource                                               | 2.5 kB     00:00
updates                                                  | 2.9 kB     00:00
Resolving Dependencies
--> Running transaction check
---> Package jq.x86_64 0:1.6-2.el7 will be installed
--> Finished Dependency Resolution


Dependencies Resolved

================================================================================
 Package        Arch               Version               Repository        Size
================================================================================
Installing:
 jq             x86_64             1.6-2.el7             epel             167 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 167 k
Installed size: 381 k
Downloading packages:
jq-1.6-2.el7.x86_64.rpm                                    | 167 kB   00:15
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jq-1.6-2.el7.x86_64                                          1/1
  Verifying  : jq-1.6-2.el7.x86_64                                          1/1

Installed:
  jq.x86_64 0:1.6-2.el7

Complete!

To print the entire object, use jq .

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .
{
  "status": "success",
  "data": [
    {
      "id": "1",
      "employee_name": "Tiger Nixon",
      "employee_salary": "320800",
      "employee_age": "61",
      "profile_image": ""
    },
    {
      "id": "2",
      "employee_name": "Garrett Winters",
      "employee_salary": "170750",
      "employee_age": "63",
      "profile_image": ""
    },
    {
      "id": "3",
      "employee_name": "Ashton Cox",
      "employee_salary": "86000",
      "employee_age": "66",
      "profile_image": ""
    },
    {
      "id": "4",
      "employee_name": "Cedric Kelly",
      "employee_salary": "433060",
      "employee_age": "22",
      "profile_image": ""
    },
    {
      "id": "5",
      "employee_name": "Airi Satou",
      "employee_salary": "162700",
      "employee_age": "33",
      "profile_image": ""
    },
    {
      "id": "6",
      "employee_name": "Brielle Williamson",
      "employee_salary": "372000",
      "employee_age": "61",
      "profile_image": ""
    },
    {
      "id": "7",
      "employee_name": "Herrod Chandler",
      "employee_salary": "137500",
      "employee_age": "59",
      "profile_image": ""
    },
    {
      "id": "8",
      "employee_name": "Rhona Davidson",
      "employee_salary": "327900",
      "employee_age": "55",
      "profile_image": ""
    },
    {
      "id": "9",
      "employee_name": "Colleen Hurst",
      "employee_salary": "205500",
      "employee_age": "39",
      "profile_image": ""
    },
    {
      "id": "10",
      "employee_name": "Sonya Frost",
      "employee_salary": "103600",
      "employee_age": "23",
      "profile_image": ""
    },
    {
      "id": "11",
      "employee_name": "Jena Gaines",
      "employee_salary": "90560",
      "employee_age": "30",
      "profile_image": ""
    },
    {
      "id": "12",
      "employee_name": "Quinn Flynn",
      "employee_salary": "342000",
      "employee_age": "22",
      "profile_image": ""
    },
    {
      "id": "13",
      "employee_name": "Charde Marshall",
      "employee_salary": "470600",
      "employee_age": "36",
      "profile_image": ""
    },
    {
      "id": "14",
      "employee_name": "Haley Kennedy",
      "employee_salary": "313500",
      "employee_age": "43",
      "profile_image": ""
    },
    {
      "id": "15",
      "employee_name": "Tatyana Fitzpatrick",
      "employee_salary": "385750",
      "employee_age": "19",
      "profile_image": ""
    },
    {
      "id": "16",
      "employee_name": "Michael Silva",
      "employee_salary": "198500",
      "employee_age": "66",
      "profile_image": ""
    },
    {
      "id": "17",
      "employee_name": "Paul Byrd",
      "employee_salary": "725000",
      "employee_age": "64",
      "profile_image": ""
    },
    {
      "id": "18",
      "employee_name": "Gloria Little",
      "employee_salary": "237500",
      "employee_age": "59",
      "profile_image": ""
    },
    {
      "id": "19",
      "employee_name": "Bradley Greer",
      "employee_salary": "132000",
      "employee_age": "41",
      "profile_image": ""
    },
    {
      "id": "20",
      "employee_name": "Dai Rios",
      "employee_salary": "217500",
      "employee_age": "35",
      "profile_image": ""
    },
    {
      "id": "21",
      "employee_name": "Jenette Caldwell",
      "employee_salary": "345000",
      "employee_age": "30",
      "profile_image": ""
    },
    {
      "id": "22",
      "employee_name": "Yuri Berry",
      "employee_salary": "675000",
      "employee_age": "40",
      "profile_image": ""
    },
    {
      "id": "23",
      "employee_name": "Caesar Vance",
      "employee_salary": "106450",
      "employee_age": "21",
      "profile_image": ""
    },
    {
      "id": "24",
      "employee_name": "Doris Wilder",
      "employee_salary": "85600",
      "employee_age": "23",
      "profile_image": ""
    }
  ]
}

Accessing data values:
Work with Object

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .status
"success"

Work with Array
All items of Array:

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .data[].employee_name
"Tiger Nixon"
"Garrett Winters"
"Ashton Cox"
"Cedric Kelly"
"Airi Satou"
"Brielle Williamson"
"Herrod Chandler"
"Rhona Davidson"
"Colleen Hurst"
"Sonya Frost"
"Jena Gaines"
"Quinn Flynn"
"Charde Marshall"
"Haley Kennedy"
"Tatyana Fitzpatrick"
"Michael Silva"
"Paul Byrd"
"Gloria Little"
"Bradley Greer"
"Dai Rios"
"Jenette Caldwell"
"Yuri Berry"
"Caesar Vance"
"Doris Wilder"

Some items of Array

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .data[10,-1].employee_name
"Jena Gaines"
"Doris Wilder"

An item

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .data[-1].employee_name
"Doris Wilder"

A slice of Array

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .data[21:-1]
[
  {
    "id": "22",
    "employee_name": "Yuri Berry",
    "employee_salary": "675000",
    "employee_age": "40",
    "profile_image": ""
  },
  {
    "id": "23",
    "employee_name": "Caesar Vance",
    "employee_salary": "106450",
    "employee_age": "21",
    "profile_image": ""
  }
]
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq .data[21:][].employee_name
"Yuri Berry"
"Caesar Vance"
"Doris Wilder"
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[21:-1][] | .employee_name'
"Yuri Berry"
"Caesar Vance"

jq functions
Function length

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[21:-1][] | length'
5
5
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[21:-1][] | .employee_name | length'
10
12

Function keys

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[21:-1][] | keys'
[
  "employee_age",
  "employee_name",
  "employee_salary",
  "id",
  "profile_image"
]
[
  "employee_age",
  "employee_name",
  "employee_salary",
  "id",
  "profile_image"
]
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data | keys'
[
  0,
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
  11,
  12,
  13,
  14,
  15,
  16,
  17,
  18,
  19,
  20,
  21,
  22,
  23
]

Function has

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq 'has("success")'
false
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq 'has("status")'
true
[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data | has(23)'
true

Function map

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data | map(has("id"))'
[
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true
]

Function tonumber

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[0].employee_age|tonumber'
61

Function del

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq 'del(.data[:22])'
{
  "status": "success",
  "data": [
    {
      "id": "23",
      "employee_name": "Caesar Vance",
      "employee_salary": "106450",
      "employee_age": "21",
      "profile_image": ""
    },
    {
      "id": "24",
      "employee_name": "Doris Wilder",
      "employee_salary": "85600",
      "employee_age": "23",
      "profile_image": ""
    }
  ]
}

Creating Arrays

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '[.data[0],.data[1]]'
[
  {
    "id": "1",
    "employee_name": "Tiger Nixon",
    "employee_salary": "320800",
    "employee_age": "61",
    "profile_image": ""
  },
  {
    "id": "2",
    "employee_name": "Garrett Winters",
    "employee_salary": "170750",
    "employee_age": "63",
    "profile_image": ""
  }
]

Modifying Results

[root@tutorialspots ~]# curl -s http://dummy.restapiexample.com/api/v1/employees | jq '.data[0].employee_age|tonumber|. - 10'
51

You can use function del to modify results

Full jq documentation: JQ(1) Manual Page

Leave a Reply