Skip to main content
Formatting
Source Link
AdminBee
  • 23.6k
  • 25
  • 56
  • 78

Give a try with this code to save each element with names 0.json0.json, 1.json & 2.json1.json and 2.json. FYI, it can work for any number of jsonJSON items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 
for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 
$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 
j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 
jq ".[$j]" sample.json > $j.json; 

to save literally to x.jsonx.json, y.jsony.json and z.jsonz.json:

for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 
for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 

Give a try with this code to save each element with names 0.json, 1.json & 2.json. FYI, it can work for any number of json items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 

to save literally to x.json, y.json and z.json:

for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 

Give a try with this code to save each element with names 0.json, 1.json and 2.json. FYI, it can work for any number of JSON items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 

to save literally to x.json, y.json and z.json:

for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 
added a direct answer to store result as requested.
Source Link

Give a try with this code to save each element with names 0.json, 1.json & 2.json. FYI, it can work for any number of json items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 

to save literally to x.json, y.json and z.json:

for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 

Give a try with this code to save each element with names 0.json, 1.json & 2.json. FYI, it can work for any number of json items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 

Give a try with this code to save each element with names 0.json, 1.json & 2.json. FYI, it can work for any number of json items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json; 

to save literally to x.json, y.json and z.json:

for i in x y z; do \ jq ".[$count]" sample.json > $i.json; \ count=$( expr $count + 1 ) \ done 
Source Link

Give a try with this code to save each element with names 0.json, 1.json & 2.json. FYI, it can work for any number of json items in an array.

for i in $(seq $(jq '.|length' sample.json)); do \ j=$( expr $i - 1 ); \ jq ".[$j]" sample.json > $j.json; \ done 

Explanation:

below line finds the length of array to name the objects:

$(seq $(jq '.|length' sample.json)) 

Since the array starts with 0, lets fix the output file name

j=$( expr $i - 1 ); 

below lines fetches one element from json document and save to file

jq ".[$j]" sample.json > $j.json;