|= .+过滤器中的部件将新元素添加到现有数组。您可以使用
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条)