Json

JQ中的豎管和並置有什麼區別?

  • July 2, 2019

我的程序返回JSONstdout它是一個帶有operations欄位的對象,其中包含一個操作列表。每個操作包含欄位,包括id欄位,例如:

{
   "operations": [
       {
           "id": "694ef895-793f4631-41103e8-8ab59e66",

我正在嘗試從中獲取 id 列表jq

如果我在寫

jq '.operations | .[] | .id'

它有效,如果我正在寫作

jq '.operations | .[].id'

它也有效。

即並列與|此處相同。

但如果我寫

jq '.operations.[].id'

它失敗並顯示消息

jq:錯誤:語法錯誤,意外'

$$ ‘, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at , line 1: .operations.[ $$.id jq: 1 編譯錯誤

為什麼以及如何解決?

只要您只比較.foo.barand |,就沒有區別,正如手冊頁在“基本過濾器”下所說:

形式的過濾器.foo.bar等價於.foo|.bar

是的,你需要.operations[].id在你的情況下。

引用自:https://unix.stackexchange.com/questions/528021