2017-02-15 72 views
12

我有一個json文件,我想在其上追加一個新的對象數組。我正在嘗試使用jq命令行。正如我在看互聯網這個命令是這樣做的,不僅在Json文件的查詢對象。但我無法弄清楚如何在jq上使用ADD命令。 我的JSON文件是報告-2017-01-07.json >>使用jq向現有JSON數組添加新元素

{ 
    "report":"1.0", 
    "data":{ 
     "date":"2010-01-07", 
     "messages":[ 
     { 
      "date":"2010-01-07T19:58:42.949Z", 
      "xml":"xml_samplesheet_2017_01_07_run_09.xml", 
      "status":"OK", 
      "message":"metadata loaded into iRODS successfully" 
     }, 
     { 
      "date":"2010-01-07T20:22:46.949Z", 
      "xml":"xml_samplesheet_2017_01_07_run_09.xml", 
      "status":"NOK", 
      "message":"metadata duplicated into iRODS" 
     }, 
     { 
      "date":"2010-01-07T22:11:55.949Z", 
      "xml":"xml_samplesheet_2017_01_07_run_09.xml", 
      "status":"NOK", 
      "message":"metadata was not validated by XSD schema" 
     } 
     ] 
    } 
} 

我使用下面的命令>>

$ cat report-2017-01-07.json | jq -s '.data.messages {"date": "2010-01-07T19:55:99.999Z", "xml": "xml_samplesheet_2017_01_07_run_09.xml", "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}' 
jq: error: syntax error, unexpected '{', expecting $end (Unix shell quoting issues?) at <top-level>, line 1: 
.data.messages {"date": "2010-01-07T19:55:99.999Z", "xml": "xml_samplesheet_2017_01_07_run_09.xml", "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}    
jq: 1 compile error 

我已經嘗試過其他命令的組合,但我總是結束錯誤編譯。有關於此的任何暗示? 謝謝!

輸出必須像>>

{ 
    "report": "1.0", 
    "data": { 
     "date": "2010-01-07", 
     "messages": [{ 
      "date": "2010-01-07T19:58:42.949Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "OK", 
      "message": "metadata loaded into iRODS successfully" 
     }, { 
      "date": "2010-01-07T20:22:46.949Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "NOK", 
      "message": "metadata duplicated into iRODS" 
     }, { 
      "date": "2010-01-07T22:11:55.949Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "NOK", 
      "message": "metadata was not validated by XSD schema" 
     }, { 
      "date": "2010-01-07T19:55:99.999Z", 
      "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
      "status": "OKKKKKKK", 
      "message": "metadata loaded into iRODS successfullyyyyy" 
     }] 
    } 
} 
+0

不知道這口井,但難道不是.data.messages是arrray的問題嗎?所以你需要類似.data.messages的東西。[] –

+0

對不起,沒有工作 –

+0

好吧 - 閱讀文檔並安裝jq(在這裏有Windows)並嘗試 - 像你一樣得到同樣的錯誤。看起來像Windows shell有問題的方式,它將雙引號輸入到跳轉jq的流中。無法獲得任何工作,因此無法回答您的問題,但您可能需要查看jq命令中的雙引號。所以''日期''會變成'\「日期\」等 –

回答

15

|= .+部分在過濾器增加了一個新元素添加到現有的陣列。可以用濾波器使用jq作爲

jq '.data.messages[3] |= . + {"date": "2010-01-07T19:55:99.999Z", "xml": "xml_samplesheet_2017_01_07_run_09.xml", "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}' inputJson 

爲了避免使用硬編碼的長度值3並動態添加新的元件,用. | length它返回長度,其可以被用作下一個數組索引,即

jq '.data.messages[.data.messages| length] |= . + {"date": "2010-01-07T19:55:99.999Z", "xml": "xml_samplesheet_2017_01_07_run_09.xml", "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}' inputJson 

(或)按在評論中峯值的建議下,單獨使用+=操作

jq '.data.messages += [{"date": "2010-01-07T19:55:99.999Z", "xml": "xml_samplesheet_2017_01_07_run_09.xml", "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}]' 

可根據您的需要生成輸出。

{ 
    "report": "1.0", 
    "data": { 
    "date": "2010-01-07", 
    "messages": [ 
     { 
     "date": "2010-01-07T19:58:42.949Z", 
     "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
     "status": "OK", 
     "message": "metadata loaded into iRODS successfully" 
     }, 
     { 
     "date": "2010-01-07T20:22:46.949Z", 
     "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
     "status": "NOK", 
     "message": "metadata duplicated into iRODS" 
     }, 
     { 
     "date": "2010-01-07T22:11:55.949Z", 
     "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
     "status": "NOK", 
     "message": "metadata was not validated by XSD schema" 
     }, 
     { 
     "date": "2010-01-07T19:55:99.999Z", 
     "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
     "status": "OKKK", 
     "message": "metadata loaded into iRODS successfullyyyyy" 
     } 
    ] 
    } 
} 

使用jq-play幹運行jq-filter和優化你想要的任何方式。

+0

由於目標只是追加一個元素,所以最好使用+ =,但值得注意的是'.data.messages [3] | =。 + X'可以簡化爲'.data.messages [3] = X',因爲RHS上的'.'實際上只是'null'。 – peak

+0

(這裏是Windows JQ用戶):我想知道是否有可能將這個新數據寫入同一個(輸入)文件,而不是創建一個需要重新命名的臨時文件.. –

+0

@ script'n'code - 使用「海綿」可能仍然是最好的選擇,如果你有它或可以安裝它(它是moreutils的一部分)。 – peak

12

而不是在這裏使用|=,它是更好的使用+=

.data.messages += [{"date": "2010-01-07T19:55:99.999Z", 
    "xml": "xml_samplesheet_2017_01_07_run_09.xml", 
    "status": "OKKK", "message": "metadata loaded into iRODS successfullyyyyy"}]