7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jqの基本的な使い方

7
Last updated at Posted at 2016-06-15

対象データ

sample.json
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

※参照元: JSON Example

目的別コマンド例

全てを表示

$ cat sample.json | jq "."
{
  "menu": {
    "popup": {
      "menuitem": [
        {
          "onclick": "CreateNewDoc()",
          "value": "New"
        },
        {
          "onclick": "OpenDoc()",
          "value": "Open"
        },
        {
          "onclick": "CloseDoc()",
          "value": "Close"
        }
      ]
    },
    "value": "File",
    "id": "file"
  }
}

menuの中身表示

$ cat sample.json | jq ".menu"
{
  "popup": {
    "menuitem": [
      {
        "onclick": "CreateNewDoc()",
        "value": "New"
      },
      {
        "onclick": "OpenDoc()",
        "value": "Open"
      },
      {
        "onclick": "CloseDoc()",
        "value": "Close"
      }
    ]
  },
  "value": "File",
  "id": "file"
}

menuitemの0番目の中身表示

$ cat sample.json | jq ".menu.popup.menuitem[0]"
{
  "onclick": "CreateNewDoc()",
  "value": "New"
}

menuitemの0番目のvalueを表示

$ cat sample.json | jq ".menu.popup.menuitem[0].value"
"New"

menuitemの全ての要素のvalueを表示

$ cat sample.json | jq ".menu.popup.menuitem[].value"
"New"
"Open"
"Close"
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?