Shell script to join 5 or more json files together

Richard picture Richard · Apr 30, 2014 · Viewed 10.6k times · Source

I am working on a project that has a lot of json documents in a large file lets call it manifest.json

The files have titles such as

a-11.json

{"id":"a-11",
 "name":"XN0",
  "code":"H3A8FF82820F"
  "status":"live"
} 

a-03.json

{"id":"a-03",
 "name":"PF1",
  "code":"FFFF82820F"
  "status":"live"
}

a-09.json

{"id":"a-09",
 "name":"PF1",
 "code":"FFFF82820F"
 "status":"live"
} 

What I want a shell script to do is to concatenate them all in alpha order and I am also need to wrap them like this: [ {json doc}, {json doc}, {json doc ] with a sq bracket seperated with a , so that it looks like the code below-

The join command only connects two files so thats not going to work and I have tried a combination of cat and ls but it all goes a bit wrong. I am trying to use the Linux environment rather than the MS environment here.

manifest.json

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

]

The command

cat a-*.json > manifest.json

Gives me the following with the a-11.json doc at the top, any help appreciated.

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

]

Answer

jfs picture jfs · Apr 30, 2014

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"
  }
]