Finding all the JSONS file in directory and combining it in array in bash
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
It's not clear to me what you wantresult.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 fromfind
into an array?
– RoadRunner
Nov 19 at 8:38
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
linux bash find command
edited Nov 19 at 8:14
asked Nov 19 at 8:01
Anurag Mishra
112
112
It's not clear to me what you wantresult.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 fromfind
into an array?
– RoadRunner
Nov 19 at 8:38
add a comment |
It's not clear to me what you wantresult.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 fromfind
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
add a comment |
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 ]
add a comment |
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.
add a comment |
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 ]
add a comment |
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 ]
add a comment |
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 ]
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 ]
answered Nov 19 at 8:29
choroba
152k14138200
152k14138200
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 at 6:27
user7154703
add a comment |
add a comment |
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.
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%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
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
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