Join an array to create JSON dynamically

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











up vote
2
down vote

favorite












Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.



Say I have an array like this:



 value1="foo"
value2="bar"
arr=("key1" "$value1" "key2" "$value2")


Is there a way to somehow join the array with ":" and "," characters.



The only thing I can think of is a loop where you add the right characters, something like this:



data="";

for i in "$arr[@]"; do
data="$data"$i""
done






share|improve this question

























    up vote
    2
    down vote

    favorite












    Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.



    Say I have an array like this:



     value1="foo"
    value2="bar"
    arr=("key1" "$value1" "key2" "$value2")


    Is there a way to somehow join the array with ":" and "," characters.



    The only thing I can think of is a loop where you add the right characters, something like this:



    data="";

    for i in "$arr[@]"; do
    data="$data"$i""
    done






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.



      Say I have an array like this:



       value1="foo"
      value2="bar"
      arr=("key1" "$value1" "key2" "$value2")


      Is there a way to somehow join the array with ":" and "," characters.



      The only thing I can think of is a loop where you add the right characters, something like this:



      data="";

      for i in "$arr[@]"; do
      data="$data"$i""
      done






      share|improve this question













      Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.



      Say I have an array like this:



       value1="foo"
      value2="bar"
      arr=("key1" "$value1" "key2" "$value2")


      Is there a way to somehow join the array with ":" and "," characters.



      The only thing I can think of is a loop where you add the right characters, something like this:



      data="";

      for i in "$arr[@]"; do
      data="$data"$i""
      done








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 22 at 2:40









      slm♦

      235k65480653




      235k65480653









      asked Apr 21 at 0:22









      Alexander Mills

      1,885929




      1,885929




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Assuming the array is correct:



          echo '['
          printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
          echo ']'


          The sed command removes the comma from the end of the last line.



          Then pass this through jq to have it formatted correctly:



           sed '$s/,$//'
          echo ']'
          | jq .


          With the given data, this produces



          [

          "key1": "foo"
          ,

          "key2": "bar"

          ]


          This will obviously treat all values as strings, which is what they are in the shell as well.






          share|improve this answer





















          • yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
            – Alexander Mills
            Apr 21 at 8:14

















          up vote
          1
          down vote













          Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.



          This is one solution, use it like so:



          join_arry_to_json a b c d
          "a":"b","c":"d"


          this will only work for strings, not for numbers or booleans.
          Now to declare a boolean or a number, you use the ^ symbol:



          ql_join_arry_to_json a ^3 b ^true c dog


          yields:



          "a":3,"b":true,"c":"dog"


          the code is here:
          https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f439046%2fjoin-an-array-to-create-json-dynamically%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Assuming the array is correct:



            echo '['
            printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
            echo ']'


            The sed command removes the comma from the end of the last line.



            Then pass this through jq to have it formatted correctly:



             sed '$s/,$//'
            echo ']'
            | jq .


            With the given data, this produces



            [

            "key1": "foo"
            ,

            "key2": "bar"

            ]


            This will obviously treat all values as strings, which is what they are in the shell as well.






            share|improve this answer





















            • yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
              – Alexander Mills
              Apr 21 at 8:14














            up vote
            1
            down vote



            accepted










            Assuming the array is correct:



            echo '['
            printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
            echo ']'


            The sed command removes the comma from the end of the last line.



            Then pass this through jq to have it formatted correctly:



             sed '$s/,$//'
            echo ']'
            | jq .


            With the given data, this produces



            [

            "key1": "foo"
            ,

            "key2": "bar"

            ]


            This will obviously treat all values as strings, which is what they are in the shell as well.






            share|improve this answer





















            • yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
              – Alexander Mills
              Apr 21 at 8:14












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Assuming the array is correct:



            echo '['
            printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
            echo ']'


            The sed command removes the comma from the end of the last line.



            Then pass this through jq to have it formatted correctly:



             sed '$s/,$//'
            echo ']'
            | jq .


            With the given data, this produces



            [

            "key1": "foo"
            ,

            "key2": "bar"

            ]


            This will obviously treat all values as strings, which is what they are in the shell as well.






            share|improve this answer













            Assuming the array is correct:



            echo '['
            printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
            echo ']'


            The sed command removes the comma from the end of the last line.



            Then pass this through jq to have it formatted correctly:



             sed '$s/,$//'
            echo ']'
            | jq .


            With the given data, this produces



            [

            "key1": "foo"
            ,

            "key2": "bar"

            ]


            This will obviously treat all values as strings, which is what they are in the shell as well.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 21 at 7:59









            Kusalananda

            102k13199315




            102k13199315











            • yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
              – Alexander Mills
              Apr 21 at 8:14
















            • yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
              – Alexander Mills
              Apr 21 at 8:14















            yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
            – Alexander Mills
            Apr 21 at 8:14




            yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
            – Alexander Mills
            Apr 21 at 8:14












            up vote
            1
            down vote













            Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.



            This is one solution, use it like so:



            join_arry_to_json a b c d
            "a":"b","c":"d"


            this will only work for strings, not for numbers or booleans.
            Now to declare a boolean or a number, you use the ^ symbol:



            ql_join_arry_to_json a ^3 b ^true c dog


            yields:



            "a":3,"b":true,"c":"dog"


            the code is here:
            https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab






            share|improve this answer



























              up vote
              1
              down vote













              Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.



              This is one solution, use it like so:



              join_arry_to_json a b c d
              "a":"b","c":"d"


              this will only work for strings, not for numbers or booleans.
              Now to declare a boolean or a number, you use the ^ symbol:



              ql_join_arry_to_json a ^3 b ^true c dog


              yields:



              "a":3,"b":true,"c":"dog"


              the code is here:
              https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.



                This is one solution, use it like so:



                join_arry_to_json a b c d
                "a":"b","c":"d"


                this will only work for strings, not for numbers or booleans.
                Now to declare a boolean or a number, you use the ^ symbol:



                ql_join_arry_to_json a ^3 b ^true c dog


                yields:



                "a":3,"b":true,"c":"dog"


                the code is here:
                https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab






                share|improve this answer















                Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.



                This is one solution, use it like so:



                join_arry_to_json a b c d
                "a":"b","c":"d"


                this will only work for strings, not for numbers or booleans.
                Now to declare a boolean or a number, you use the ^ symbol:



                ql_join_arry_to_json a ^3 b ^true c dog


                yields:



                "a":3,"b":true,"c":"dog"


                the code is here:
                https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Apr 22 at 6:49


























                answered Apr 21 at 0:32









                Alexander Mills

                1,885929




                1,885929






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f439046%2fjoin-an-array-to-create-json-dynamically%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)