Split a JSON file into separate files

file, jq, json, split

Solution

This should give you a start:

for f in `cat input.json | jq -r 'keys[]'` ; do
  cat input.json | jq ".$f" > $f.json
done

or when you insist on more bashy syntax like some seem to prefer:

for f in $(jq -r 'keys[]') ; do
  jq ".[\"$f\"]" < input.json > "$f.json"
done < input.json

Problem

I have a large JSON file that is an object of objects, which I would like to split into separate files name after object keys. Is it possible to achieve this using jq or any other off-the-shelf tools? The original JSON is in the following format `{ "item1": {...}, "item2": {...}, ...}` Given this input I would like to produce files item1.json, item2.json etc.

Original source