LoginSignup
0
0

jq "@" ワードが入ったプロパティ名を抽出する際に困った件

Posted at

以下のようなJsonの形式のファイルに対して、プロパティ名に'@'キーワードが入った場合に困った。

$ cat sample.json | jq
{
  "@title": "is title.",
  "message": "is message."
}

$ cat sample.json | jq .@title
jq: error: syntax error, unexpected end of file, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.@title 
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.@title
jq: 2 compile errors

解決策

文字列として扱うために[]と”(ダブルクォート)で囲む

$ cat sample.json | jq .[\"@title\"]
"is title."

複数項目の場合

$ cat sample.json | jq ".[\"@title\"],.message"
"is title."
"is message."

$ cat sample.json | jq "[.[\"@title\"],.message]|@csv"
"\"is title.\",\"is message.\""

$ cat sample.json | jq "[.[\"@title\"],.message]|@csv"
"\"is title.\",\"is message.\""

$ cat sample.json | jq "[.[\"@title\"],.message]|@csv" | sed -e 's/\"//g' | sed -e 's/\\//g'
is title.,is message.
0
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
0
0