Shell script to join 5 or more json files together Shell script to join 5 or more json files together shell shell

Shell script to join 5 or more json files together


You could use jq utility (sed for JSON data):

$ jq -s '.' a-*.json > manifest.json

manifest.json:

[  {    "status": "live",    "code": "H3A8FF82820F",    "name": "XN0",    "id": "a-11"  },  {    "status": "live",    "code": "FFFF82820F",    "name": "PF1",    "id": "a-03"  },  {    "status": "live",    "code": "FFFF82820F",    "name": "PF1",    "id": "a-09"  }]


Use the following bash script (it uses arrays which aren't standard across all shells):

#!/bin/bashshopt -s nullglobdeclare -a jsonsjsons=(a-*.json) # ${jsons[@]} now contains the list of files to concatenateecho '[' > manifest.jsonif [ ${#jsons[@]} -gt 0 ]; then # if the list is not empty  cat "${jsons[0]}" >> manifest.json # concatenate the first file to the manifest...  unset jsons[0]                     # and remove it from the list  for f in "${jsons[@]}"; do         # iterate over the rest      echo "," >>manifest.json      cat "$f" >>manifest.json  donefiecho ']' >>manifest.json             # complete the manifest