Files and folders

Pick values out of JSON with jq

Pull one field, a list of fields or a count out of a JSON file or an API reply, right in the terminal.

Fill these in once
  1. Install jq
    Terminal
    brew install jq
  2. Get one field
    Terminal
    jq '.name' data.json
  3. Get that field from every item in a list
    Terminal
    jq '.[].name' data.json

    Use this when the file starts with [ rather than {.

  4. Print it without quotes
    Terminal
    jq -r '.[].name' data.json

    -r gives plain text, handy for passing on to other commands.

  5. Count the items in a list
    Terminal
    jq 'length' data.json
  6. Use it on an API reply
    Terminal
    curl -s https://api.github.com/repos/jqlang/jq | jq '.stargazers_count'
Undo it
  1. Uninstall jq
    Terminal
    brew uninstall jq
  1. Install jqNeeds sudo
    Terminal · Ubuntu / Debian
    sudo apt install -y jq
  2. Get one field
    Terminal · Ubuntu / Debian
    jq '.name' data.json
  3. Get that field from every item in a list
    Terminal · Ubuntu / Debian
    jq '.[].name' data.json

    Use this when the file starts with [ rather than {.

  4. Print it without quotes
    Terminal · Ubuntu / Debian
    jq -r '.[].name' data.json

    -r gives plain text, handy for passing on to other commands.

  5. Count the items in a list
    Terminal · Ubuntu / Debian
    jq 'length' data.json
  6. Use it on an API reply
    Terminal · Ubuntu / Debian
    curl -s https://api.github.com/repos/jqlang/jq | jq '.stargazers_count'
Undo it
  1. Uninstall jqNeeds sudo
    Terminal · Ubuntu / Debian
    sudo apt remove -y jq
  1. Install jq
    PowerShell
    winget install jqlang.jq

    Close and reopen PowerShell afterwards.

  2. Get one field
    PowerShell
    jq '.name' data.json
  3. Get that field from every item in a list
    PowerShell
    jq '.[].name' data.json

    Use this when the file starts with [ rather than {.

  4. Print it without quotes
    PowerShell
    jq -r '.[].name' data.json
  5. Count the items in a list
    PowerShell
    jq 'length' data.json
  6. Use it on an API reply
    PowerShell
    curl.exe -s https://api.github.com/repos/jqlang/jq | jq '.stargazers_count'
Undo it
  1. Uninstall jq
    PowerShell
    winget uninstall jqlang.jq

New to JSON? I explain it in plain English on Layman Terms.