append values under element in json

Clash Royale CLAN TAG#URR8PPP
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"
]
bash shell-script awk sed jq
add a comment |
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"
]
bash shell-script awk sed jq
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08
add a comment |
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"
]
bash shell-script awk sed jq
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"
]
bash shell-script awk sed jq
bash shell-script awk sed jq
asked Feb 25 at 16:52
yaelyael
2,74222576
2,74222576
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08
add a comment |
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
With slightly different indentation.
$ sed 's/("master01.sys645.com",)/1'$(echo -e "\\n\\t"$list_of_machine// /",\\n\\t""")'/' 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"
]
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502938%2fappend-values-under-element-in-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 26 at 9:30
dirktdirkt
17.4k31338
17.4k31338
add a comment |
add a comment |
With slightly different indentation.
$ sed 's/("master01.sys645.com",)/1'$(echo -e "\\n\\t"$list_of_machine// /",\\n\\t""")'/' 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"
]
add a comment |
With slightly different indentation.
$ sed 's/("master01.sys645.com",)/1'$(echo -e "\\n\\t"$list_of_machine// /",\\n\\t""")'/' 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"
]
add a comment |
With slightly different indentation.
$ sed 's/("master01.sys645.com",)/1'$(echo -e "\\n\\t"$list_of_machine// /",\\n\\t""")'/' 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 slightly different indentation.
$ sed 's/("master01.sys645.com",)/1'$(echo -e "\\n\\t"$list_of_machine// /",\\n\\t""")'/' 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"
]
answered Feb 25 at 17:09
FreddyFreddy
1,339210
1,339210
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502938%2fappend-values-under-element-in-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Without knowing the data super well any attempts with awk and/or sed and scripting are likely to fail ... try these: stackoverflow.com/questions/42245288/… stackoverflow.com/questions/51147753/… stackoverflow.com/questions/48963263/…
– tink
Feb 25 at 17:04
the target is to append the list of machine under - HTTP_SERVER
– yael
Feb 25 at 17:05
And it's guaranteed to have a line ` "master01.sys645.com",` ? Btw, that trailing comma is not valid JSON ;)
– tink
Feb 25 at 17:08