4
0

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でキーを指定して深い位置にある要素を取得する

Last updated at Posted at 2019-05-25

version: jq-1.6 on mac

こういうJSONがあったとしてtestを取りたいとする。キーはユニークなものを想定。

{
  "a": "foo",
  "b": {
    "c": {
      "d": {
        "test": {
          "key": "value"
        }
      }
    }
  }
}

これで取れる。

jq '..|select(.test?)|.test' test.json

selectの?がないとCannot index string with string "test"と言われて怒られるので注意。

testと二回書くのが嫌な時はこれでもいけるけど、検索文字列が短い場合はちょっと冗長な感じかも。

jq '..|to_entries?|.[]|select(.key=="test").value' test.json
4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?