OpenStackJsonParsing

22nd June 2017 at 10:09am
Bash jq OpenStack TechnicalNotes

The jq command can be very useful when parsing JSON output on the command line or from a bash script.

In this example we extract the ID for the record named provider.

  • jq -c '.[] | select(.Name | contains("provider"))'.ID
root@server:/etc/keystone# openstack network list
+--------------------------------------+-------------+--------------------------------------+
| ID                                   | Name        | Subnets                              |
+--------------------------------------+-------------+--------------------------------------+
| 34e0623a-ef90-9911-9a22-e0c654fd3ca3 | selfservice | 99cfbc1c-0e1a-4380-8912-af634a637d7d |
| 7463a014-dd81-8811-a6f0-8e3267884317 | provider    | 33eed837-0b34-48a2-bc80-8c8c1e33cc50 |
+--------------------------------------+-------------+--------------------------------------+
root@server:/etc/keystone# openstack network list --format json
[
  {
    "Subnets": "99cfbc1c-0e1a-4380-8912-af634a637d7d", 
    "ID": "34e0623a-ef90-9911-9a22-e0c654fd3c01", 
    "Name": "selfservice"
  }, 
  {
    "Subnets": "33eed837-0b34-48a2-bc80-8c8c1e33cc50", 
    "ID": "7463a014-dd81-8811-a6f0-8e3267884309", 
    "Name": "provider"
  }
]root@server:/etc/keystone# openstack network list --format json | jq -c '.[] | select(.Name | contains("provider"))'.ID
"7463a014-dd81-4737-a6f0-8e3267884317"

Related