append values under element in json

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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










share|improve this question






















  • 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















0















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










share|improve this question






















  • 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













0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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










2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer






























    1














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






    share|improve this answer






















      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
      );



      );













      draft saved

      draft discarded


















      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









      2














      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.






      share|improve this answer



























        2














        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.






        share|improve this answer

























          2












          2








          2







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 26 at 9:30









          dirktdirkt

          17.4k31338




          17.4k31338























              1














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






              share|improve this answer



























                1














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






                share|improve this answer

























                  1












                  1








                  1







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






                  share|improve this answer













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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 25 at 17:09









                  FreddyFreddy

                  1,339210




                  1,339210



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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






                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)