@kyanny's blog

My thoughts, my life. Views/opinions are my own.

jq: オブジェクトのリストを配列に変換する

stackoverflow.com

↑に書いてあるとおりだが、

{
    "name": "John",
    "email": "john@company.com"
}
{
    "name": "Brad",
    "email": "brad@company.com"
}

[
    {
        "name": "John",
        "email": "john@company.com"
    },
    {
        "name": "Brad",
        "email": "brad@company.com"
    }
]

に変換したい。

1. jq -s にパイプする

❯ echo '{
    "name": "John",
    "email": "john@company.com"
}
{
    "name": "Brad",
    "email": "brad@company.com"
}' | jq -s
[
  {
    "name": "John",
    "email": "john@company.com"
  },
  {
    "name": "Brad",
    "email": "brad@company.com"
  }
]

2. [ ] で囲う

一回の jq コマンドだけでやりたい場合はこちら。

❯ echo '{
    "name": "John",
    "email": "john@company.com"
}
{
    "name": "Brad",
    "email": "brad@company.com"
}' | jq '[ . ]'
[
  {
    "name": "John",
    "email": "john@company.com"
  }
]
[
  {
    "name": "Brad",
    "email": "brad@company.com"
  }
]

↑は無意味な例だけど、例えば select() でフィルタした結果をもう一度配列にしたい場合などに使う。

❯ az vm image list | jq '.[] | select(.publisher == "SUSE")'
WARNING: You are viewing an offline list of images, use --all to retrieve an up-to-date list
{
  "architecture": "x64",
  "offer": "opensuse-leap-15-3",
  "publisher": "SUSE",
  "sku": "gen2",
  "urn": "SUSE:opensuse-leap-15-3:gen2:latest",
  "urnAlias": "openSUSE-Leap",
  "version": "latest"
}
{
  "architecture": "x64",
  "offer": "openSUSE-leap-15-4",
  "publisher": "SUSE",
  "sku": "gen2",
  "urn": "SUSE:openSUSE-leap-15-4:gen2:latest",
  "urnAlias": "OpenSuseLeap154Gen2",
  "version": "latest"
}
{
  "architecture": "x64",
  "offer": "sles-15-sp3",
  "publisher": "SUSE",
  "sku": "gen2",
  "urn": "SUSE:sles-15-sp3:gen2:latest",
  "urnAlias": "SLES",
  "version": "latest"
}
{
  "architecture": "x64",
  "offer": "sles-15-sp3",
  "publisher": "SUSE",
  "sku": "gen2",
  "urn": "SUSE:sles-15-sp3:gen2:latest",
  "urnAlias": "SuseSles15SP3",
  "version": "latest"
}
❯ az vm image list | jq '.[] | select(.publisher == "SUSE")' | jq -s
WARNING: You are viewing an offline list of images, use --all to retrieve an up-to-date list
[
  {
    "architecture": "x64",
    "offer": "opensuse-leap-15-3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:opensuse-leap-15-3:gen2:latest",
    "urnAlias": "openSUSE-Leap",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "openSUSE-leap-15-4",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:openSUSE-leap-15-4:gen2:latest",
    "urnAlias": "OpenSuseLeap154Gen2",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "sles-15-sp3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:sles-15-sp3:gen2:latest",
    "urnAlias": "SLES",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "sles-15-sp3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:sles-15-sp3:gen2:latest",
    "urnAlias": "SuseSles15SP3",
    "version": "latest"
  }
]
❯ az vm image list | jq '[ .[] | select(.publisher == "SUSE") ]'
WARNING: You are viewing an offline list of images, use --all to retrieve an up-to-date list
[
  {
    "architecture": "x64",
    "offer": "opensuse-leap-15-3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:opensuse-leap-15-3:gen2:latest",
    "urnAlias": "openSUSE-Leap",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "openSUSE-leap-15-4",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:openSUSE-leap-15-4:gen2:latest",
    "urnAlias": "OpenSuseLeap154Gen2",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "sles-15-sp3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:sles-15-sp3:gen2:latest",
    "urnAlias": "SLES",
    "version": "latest"
  },
  {
    "architecture": "x64",
    "offer": "sles-15-sp3",
    "publisher": "SUSE",
    "sku": "gen2",
    "urn": "SUSE:sles-15-sp3:gen2:latest",
    "urnAlias": "SuseSles15SP3",
    "version": "latest"
  }
]

でも、頻出パターンに思えるからもっと直感的で簡潔なやり方がありそうな気もするが・・・。