skip to Main Content

I am running brew list --versions to get this list:

    user@mac ~ [1]> brew list --versions
    fish 3.7.0
    ncurses 6.4
    pcre2 10.43
    syncthing 1.27.4
    appcleaner 3.6.8
    teamviewer 15.51.6

How can I put the output to a json object like this:

[
   {
      "name":"fish",
      "version":"3.7.0"
   },
   {
      "name":"ncurses",
      "version":"6.4"
   }
]

2

Answers


  1. Pipe it to this JQ command:

    jq -Rn '[inputs/" " | {name: .[0], version: .[1]}]'
    

    Like this:

    $ brew list --versions | jq -Rn '[inputs/" " | {name: .[0], version: .[1]}]'
    [
      {
        "name": "fish",
        "version": "3.7.0"
      },
      {
        "name": "ncurses",
        "version": "6.4"
      },
      {
        "name": "pcre2",
        "version": "10.43"
      },
      {
        "name": "syncthing",
        "version": "1.27.4"
      },
      {
        "name": "appcleaner",
        "version": "3.6.8"
      },
      {
        "name": "teamviewer",
        "version": "15.51.6"
      }
    ]
    
    Login or Signup to reply.
  2. You can get directly json format with brew info:

    brew info --json --installed | jq 'map({name, version:.installed[0].version})'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search