Add new element to existing JSON array with jq

前端 未结 3 1335
甜味超标
甜味超标 2020-11-30 01:44

I want to append an element to an array in a JSON file using the jq``add command, but it\'s not working.

report-2017-01-07.json file:

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 02:15

    Rather than using |=, consider using +=:

    .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"}]
    

    Prepend

    On the other hand, if (as @NicHuang asked) you want to add the JSON object to the beginning of the array, you could use the pattern:

     .data.messages |= [ _ ] + .
    

提交回复
热议问题