Home > database >  How do I transform this JSON object into smaller files using bash?
How do I transform this JSON object into smaller files using bash?

Time:02-19

The input JSON file which will be provided to the BASH script is the following:

{
    "array": [
        {
            "field1": "value1",
            "field2": "value2", 
            "field3": "value3",
            "field4": "value4",
            "field5": "value5"
        },
        {
            "field1": "value6",
            "field2": "value7", 
            "field3": "value8",
            "field4": "value9",
            "field5": "value10"
        }
    ]
}

I need to break this up into separate json files like using BASH. File1 Name: file1.json

{
   "field1": "value1",
   "field2": "value2", 
   "field3": "value3",
   "field4": "value4",
   "field5": "value5"
}

File2 Name: file2.json

{
   "field1": "value6",
   "field2": "value7", 
   "field3": "value8",
   "field4": "value9",
   "field5": "value10"
}

CodePudding user response:

The canonical answer to "how do I...JSON...bash" is the jq command. While there are probably a number of ways of tackling this, you could do something like this:

for ((i=0; i<$(jq '.array|length' data.json); i   )); do
  jq ".array[$i]" data.json > file$((i 1)).json
done

This produces:

$ ls -l file*
-rw-rw-r--. 1 lars lars 113 Feb 17 21:43 file1.json
-rw-rw-r--. 1 lars lars 114 Feb 17 21:43 file2.json

Or you can use the csplit command to split the output:

jq '.array[]' data.json | csplit -z -f file -b 'd.json' - '/^{$/' '{*}'

Given your sample input, this produces two files:

$ ls -l file*
-rw-rw-r--. 1 lars lars 113 Feb 17 21:44 file00.json
-rw-rw-r--. 1 lars lars 114 Feb 17 21:44 file01.json

CodePudding user response:

There's no need for a Bash script. With the JSON-parser you can do this with just 1 call:

$ xidel -s "input.json" -e '
  for $x at $i in $json/array?* return
  file:write(x"file{$i}.json",$x,{"method":"json","indent":true()})
'

This yields 'file1.json' and 'file2.json'.

  • Related