4
1

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] jqで特殊な文字(-, $など)を含むkeyでフィルタできないときの対処法 (If the key contains special characters)

Posted at

例えば、keyにハイフン(-)が入っているとする。

test.json
{
  "foo-bar": "valueA",
  "hoge-fuga": "valueB"
}

そのままjqすると、エラーになる。

$ cat test.json | jq '.foo-bar'
jq: error: bar/0 is not defined at <top-level>, line 1:
.foo-bar
jq: 1 compile error

対処法

マニュアルによると、

キーを""で囲めばフィルタできるようになる。

cat test.json | jq '."foo-bar"'
"valueA"

If the key contains special characters, you need to surround it with double quotes like this: ."foo-bar", or else .["foo-bar"].

参考

https://qiita.com/keigo1450/items/84e696f8be0761ed0e04
https://stedolan.github.io/jq/manual/

4
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?