Finding all the JSONS file in directory and combining it in array in bash











up vote
2
down vote

favorite
1












i wanna find all the json files file specific directory and combine it one parent result that will contain all the result data



I am trying this



 find .  -name *data.json | tee result.json


It is finding all the data but it is saving its path in result.json,how can i get the data inside all the file and get a combine JSON of all result



this command is saving a result.json file which look like this



./17-10-2018/ghatkopar/17-18/data.json
./17-10-2018/ghatkopar/18-19/data.json
./17-10-2018/ghatkopar/10-11/data.json
./17-10-2018/ghatkopar/11-12/data.json
./17-10-2018/ghatkopar/15-16/data.json
./17-10-2018/ghatkopar/19-20/data.json
./17-10-2018/ghatkopar/14-15/data.json
./17-10-2018/ghatkopar/12-13/data.json
./17-10-2018/ghatkopar/20-21/data.json
./17-10-2018/mulund-west/16-17/data.json
./17-10-2018/mulund-west/21-22/data.json
./17-10-2018/mulund-west/13-14/data.json
./17-10-2018/mulund-west/data.json
./17-10-2018/mulund-west/17-18/data.json
./17-10-2018/mulund-west/18-19/data.json
./17-10-2018/mulund-west/10-11/data.json
./17-10-2018/mulund-west/11-12/data.json
./17-10-2018/mulund-west/15-16/data.json
./17-10-2018/mulund-west/19-20/data.json
./17-10-2018/mulund-west/14-15/data.json
./17-10-2018/mulund-west/12-13/data.json
./17-10-2018/mulund-west/20-21/data.json
./17-10-2018/bhandup/16-17/data.json
./17-10-2018/bhandup/21-22/data.json
./17-10-2018/bhandup/13-14/data.json
./17-10-2018/bhandup/data.json
./17-10-2018/bhandup/17-18/data.json
./17-10-2018/bhandup/18-19/data.json
./17-10-2018/bhandup/10-11/data.json
./17-10-2018/bhandup/11-12/data.json
./17-10-2018/bhandup/15-16/data.json









share|improve this question
























  • It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
    – ruakh
    Nov 19 at 8:06










  • yeah,i wanna wrap it in array
    – Anurag Mishra
    Nov 19 at 8:07










  • Could you not just save the results from find into an array?
    – RoadRunner
    Nov 19 at 8:38















up vote
2
down vote

favorite
1












i wanna find all the json files file specific directory and combine it one parent result that will contain all the result data



I am trying this



 find .  -name *data.json | tee result.json


It is finding all the data but it is saving its path in result.json,how can i get the data inside all the file and get a combine JSON of all result



this command is saving a result.json file which look like this



./17-10-2018/ghatkopar/17-18/data.json
./17-10-2018/ghatkopar/18-19/data.json
./17-10-2018/ghatkopar/10-11/data.json
./17-10-2018/ghatkopar/11-12/data.json
./17-10-2018/ghatkopar/15-16/data.json
./17-10-2018/ghatkopar/19-20/data.json
./17-10-2018/ghatkopar/14-15/data.json
./17-10-2018/ghatkopar/12-13/data.json
./17-10-2018/ghatkopar/20-21/data.json
./17-10-2018/mulund-west/16-17/data.json
./17-10-2018/mulund-west/21-22/data.json
./17-10-2018/mulund-west/13-14/data.json
./17-10-2018/mulund-west/data.json
./17-10-2018/mulund-west/17-18/data.json
./17-10-2018/mulund-west/18-19/data.json
./17-10-2018/mulund-west/10-11/data.json
./17-10-2018/mulund-west/11-12/data.json
./17-10-2018/mulund-west/15-16/data.json
./17-10-2018/mulund-west/19-20/data.json
./17-10-2018/mulund-west/14-15/data.json
./17-10-2018/mulund-west/12-13/data.json
./17-10-2018/mulund-west/20-21/data.json
./17-10-2018/bhandup/16-17/data.json
./17-10-2018/bhandup/21-22/data.json
./17-10-2018/bhandup/13-14/data.json
./17-10-2018/bhandup/data.json
./17-10-2018/bhandup/17-18/data.json
./17-10-2018/bhandup/18-19/data.json
./17-10-2018/bhandup/10-11/data.json
./17-10-2018/bhandup/11-12/data.json
./17-10-2018/bhandup/15-16/data.json









share|improve this question
























  • It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
    – ruakh
    Nov 19 at 8:06










  • yeah,i wanna wrap it in array
    – Anurag Mishra
    Nov 19 at 8:07










  • Could you not just save the results from find into an array?
    – RoadRunner
    Nov 19 at 8:38













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





i wanna find all the json files file specific directory and combine it one parent result that will contain all the result data



I am trying this



 find .  -name *data.json | tee result.json


It is finding all the data but it is saving its path in result.json,how can i get the data inside all the file and get a combine JSON of all result



this command is saving a result.json file which look like this



./17-10-2018/ghatkopar/17-18/data.json
./17-10-2018/ghatkopar/18-19/data.json
./17-10-2018/ghatkopar/10-11/data.json
./17-10-2018/ghatkopar/11-12/data.json
./17-10-2018/ghatkopar/15-16/data.json
./17-10-2018/ghatkopar/19-20/data.json
./17-10-2018/ghatkopar/14-15/data.json
./17-10-2018/ghatkopar/12-13/data.json
./17-10-2018/ghatkopar/20-21/data.json
./17-10-2018/mulund-west/16-17/data.json
./17-10-2018/mulund-west/21-22/data.json
./17-10-2018/mulund-west/13-14/data.json
./17-10-2018/mulund-west/data.json
./17-10-2018/mulund-west/17-18/data.json
./17-10-2018/mulund-west/18-19/data.json
./17-10-2018/mulund-west/10-11/data.json
./17-10-2018/mulund-west/11-12/data.json
./17-10-2018/mulund-west/15-16/data.json
./17-10-2018/mulund-west/19-20/data.json
./17-10-2018/mulund-west/14-15/data.json
./17-10-2018/mulund-west/12-13/data.json
./17-10-2018/mulund-west/20-21/data.json
./17-10-2018/bhandup/16-17/data.json
./17-10-2018/bhandup/21-22/data.json
./17-10-2018/bhandup/13-14/data.json
./17-10-2018/bhandup/data.json
./17-10-2018/bhandup/17-18/data.json
./17-10-2018/bhandup/18-19/data.json
./17-10-2018/bhandup/10-11/data.json
./17-10-2018/bhandup/11-12/data.json
./17-10-2018/bhandup/15-16/data.json









share|improve this question















i wanna find all the json files file specific directory and combine it one parent result that will contain all the result data



I am trying this



 find .  -name *data.json | tee result.json


It is finding all the data but it is saving its path in result.json,how can i get the data inside all the file and get a combine JSON of all result



this command is saving a result.json file which look like this



./17-10-2018/ghatkopar/17-18/data.json
./17-10-2018/ghatkopar/18-19/data.json
./17-10-2018/ghatkopar/10-11/data.json
./17-10-2018/ghatkopar/11-12/data.json
./17-10-2018/ghatkopar/15-16/data.json
./17-10-2018/ghatkopar/19-20/data.json
./17-10-2018/ghatkopar/14-15/data.json
./17-10-2018/ghatkopar/12-13/data.json
./17-10-2018/ghatkopar/20-21/data.json
./17-10-2018/mulund-west/16-17/data.json
./17-10-2018/mulund-west/21-22/data.json
./17-10-2018/mulund-west/13-14/data.json
./17-10-2018/mulund-west/data.json
./17-10-2018/mulund-west/17-18/data.json
./17-10-2018/mulund-west/18-19/data.json
./17-10-2018/mulund-west/10-11/data.json
./17-10-2018/mulund-west/11-12/data.json
./17-10-2018/mulund-west/15-16/data.json
./17-10-2018/mulund-west/19-20/data.json
./17-10-2018/mulund-west/14-15/data.json
./17-10-2018/mulund-west/12-13/data.json
./17-10-2018/mulund-west/20-21/data.json
./17-10-2018/bhandup/16-17/data.json
./17-10-2018/bhandup/21-22/data.json
./17-10-2018/bhandup/13-14/data.json
./17-10-2018/bhandup/data.json
./17-10-2018/bhandup/17-18/data.json
./17-10-2018/bhandup/18-19/data.json
./17-10-2018/bhandup/10-11/data.json
./17-10-2018/bhandup/11-12/data.json
./17-10-2018/bhandup/15-16/data.json






linux bash find command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 8:14

























asked Nov 19 at 8:01









Anurag Mishra

112




112












  • It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
    – ruakh
    Nov 19 at 8:06










  • yeah,i wanna wrap it in array
    – Anurag Mishra
    Nov 19 at 8:07










  • Could you not just save the results from find into an array?
    – RoadRunner
    Nov 19 at 8:38


















  • It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
    – ruakh
    Nov 19 at 8:06










  • yeah,i wanna wrap it in array
    – Anurag Mishra
    Nov 19 at 8:07










  • Could you not just save the results from find into an array?
    – RoadRunner
    Nov 19 at 8:38
















It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
– ruakh
Nov 19 at 8:06




It's not clear to me what you want result.json to contain; there are many different ways to combine many JSON values into a single value. Do you just want them wrapped in an array?
– ruakh
Nov 19 at 8:06












yeah,i wanna wrap it in array
– Anurag Mishra
Nov 19 at 8:07




yeah,i wanna wrap it in array
– Anurag Mishra
Nov 19 at 8:07












Could you not just save the results from find into an array?
– RoadRunner
Nov 19 at 8:38




Could you not just save the results from find into an array?
– RoadRunner
Nov 19 at 8:38












2 Answers
2






active

oldest

votes

















up vote
1
down vote













You need to cat each file, not output its name. You also need to output a comma before each json except the first one, and the enclosing square brackets:



#! /bin/bash
printf [
separator=""
find . -name '*data.json' -print0 | while IFS= read -d '' -r j ; do
printf $separator
separator=,
cat "$j"
done
printf ]





share|improve this answer




























    up vote
    0
    down vote













    This command will search all data*.json files in current and child folders and combine them as a results.json ( array )



    find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



    I have done basic testing using below commands



    echo {"name":"Alex", age:24, city:"Tokyo"} > sample.json



    for i in {1..10} ; do cp sample.json data$i.json ; done



    find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



    If you find its not working for a specific case, I will try to fix it. But as of now it will not work for nested json or json with array.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53370494%2ffinding-all-the-jsons-file-in-directory-and-combining-it-in-array-in-bash%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








      up vote
      1
      down vote













      You need to cat each file, not output its name. You also need to output a comma before each json except the first one, and the enclosing square brackets:



      #! /bin/bash
      printf [
      separator=""
      find . -name '*data.json' -print0 | while IFS= read -d '' -r j ; do
      printf $separator
      separator=,
      cat "$j"
      done
      printf ]





      share|improve this answer

























        up vote
        1
        down vote













        You need to cat each file, not output its name. You also need to output a comma before each json except the first one, and the enclosing square brackets:



        #! /bin/bash
        printf [
        separator=""
        find . -name '*data.json' -print0 | while IFS= read -d '' -r j ; do
        printf $separator
        separator=,
        cat "$j"
        done
        printf ]





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          You need to cat each file, not output its name. You also need to output a comma before each json except the first one, and the enclosing square brackets:



          #! /bin/bash
          printf [
          separator=""
          find . -name '*data.json' -print0 | while IFS= read -d '' -r j ; do
          printf $separator
          separator=,
          cat "$j"
          done
          printf ]





          share|improve this answer












          You need to cat each file, not output its name. You also need to output a comma before each json except the first one, and the enclosing square brackets:



          #! /bin/bash
          printf [
          separator=""
          find . -name '*data.json' -print0 | while IFS= read -d '' -r j ; do
          printf $separator
          separator=,
          cat "$j"
          done
          printf ]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 8:29









          choroba

          152k14138200




          152k14138200
























              up vote
              0
              down vote













              This command will search all data*.json files in current and child folders and combine them as a results.json ( array )



              find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



              I have done basic testing using below commands



              echo {"name":"Alex", age:24, city:"Tokyo"} > sample.json



              for i in {1..10} ; do cp sample.json data$i.json ; done



              find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



              If you find its not working for a specific case, I will try to fix it. But as of now it will not work for nested json or json with array.






              share|improve this answer

























                up vote
                0
                down vote













                This command will search all data*.json files in current and child folders and combine them as a results.json ( array )



                find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                I have done basic testing using below commands



                echo {"name":"Alex", age:24, city:"Tokyo"} > sample.json



                for i in {1..10} ; do cp sample.json data$i.json ; done



                find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                If you find its not working for a specific case, I will try to fix it. But as of now it will not work for nested json or json with array.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This command will search all data*.json files in current and child folders and combine them as a results.json ( array )



                  find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                  I have done basic testing using below commands



                  echo {"name":"Alex", age:24, city:"Tokyo"} > sample.json



                  for i in {1..10} ; do cp sample.json data$i.json ; done



                  find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                  If you find its not working for a specific case, I will try to fix it. But as of now it will not work for nested json or json with array.






                  share|improve this answer












                  This command will search all data*.json files in current and child folders and combine them as a results.json ( array )



                  find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                  I have done basic testing using below commands



                  echo {"name":"Alex", age:24, city:"Tokyo"} > sample.json



                  for i in {1..10} ; do cp sample.json data$i.json ; done



                  find . -name data*.json -type f | xargs cat | sed -e s/}/},/g -e $s/,$/]/ -e 1s/^/[/ > results.json



                  If you find its not working for a specific case, I will try to fix it. But as of now it will not work for nested json or json with array.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 6:27







                  user7154703





































                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2fstackoverflow.com%2fquestions%2f53370494%2ffinding-all-the-jsons-file-in-directory-and-combining-it-in-array-in-bash%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy