Artificial intelligent assistant

append values under element in json the following parameter include list values ( list of machines ) for example echo $list_of_machine worker01.sys645.com worker02.sys645.com worker03.sys645.com worker04.sys645.com worker05.sys645.com we have this standard json { "MNN_server": [ "master02.sys645.com" ], "HTTP_SERVER": [ "master01.sys645.com", ] } how to append the values - $list_of_machine under **HTTP_SERVER** element , as the following expected results { "MNN_server": [ "master02.sys645.com" ], "HTTP_SERVER": [ "master01.sys645.com", "worker01.sys645.com", "worker02.sys645.com", "worker03.sys645.com", "worker04.sys645.com", "worker05.sys645.com" ] }

Note that your "standard JSON" isn't actually JSON (there's an extra comma before the closing `]` in `HTTP_SERVER`).

Also, `sed` will only work if the source is formatted in the way you need it, it won't work for arbitrary JSON (which may not have line breaks).

So a better solution is to use `jq`, as in


$ jq --arg list "$list_of_machine" '.HTTP_SERVER|=.+($list|split(" "))' < your_file.json
{
"MNN_server": [
"master02.sys645.com"
],
"HTTP_SERVER": [
"master01.sys645.com",
"worker01.sys645.com",
"worker02.sys645.com",
"worker03.sys645.com",
"worker04.sys645.com",
"worker05.sys645.com"
]
}


with a well-formed `your_file.json`, of course.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy bf70246fc1c4b96f6fcf757658501c58