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