我有一個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"
}]
}
}
不知道這口井,但難道不是.data.messages是arrray的問題嗎?所以你需要類似.data.messages的東西。[] –
對不起,沒有工作 –
好吧 - 閱讀文檔並安裝jq(在這裏有Windows)並嘗試 - 像你一樣得到同樣的錯誤。看起來像Windows shell有問題的方式,它將雙引號輸入到跳轉jq的流中。無法獲得任何工作,因此無法回答您的問題,但您可能需要查看jq命令中的雙引號。所以''日期''會變成'\「日期\」等 –