Combine Multiple JSON Files Using JQ in Shell Scripting

In this tutorial, we will learn to combine multiple JSON files using JQ in Shell Scripting.

In Linux, it’s common to work with multiple JSON files that need to be combined into a single file for processing. JQ is a powerful command-line tool that can help us accomplish this task. In this tutorial, we’ll explore how to use JQ to combine multiple JSON files.

Combining Multiple JSON Files

Suppose we have three JSON files with the following data:

file1.json:

{
  "name": "John",
  "age": 25
}

file2.json:

{
  "name": "Jane",
  "age": 30
}

file3.json:

{
  "name": "Bob",
  "age": 35
}

To combine these three files into a single JSON file, we can use the following command:

jq -s '.' file1.json file2.json file3.json > combined.json

Let’s break this command down:

  • The -s option tells JQ to read the input as a stream of JSON objects rather than a single object.
  • The . operator is used to concatenate the input JSON objects.
  • The file names of the input JSON files are listed after the JQ command.
  • The > symbol redirects the output to a file named combined.json.

The resulting combined.json file will contain the following data:

[
  {
    "name": "John",
    "age": 25
  },
  {
    "name": "Jane",
    "age": 30
  },
  {
    "name": "Bob",
    "age": 35
  }
]

Note that the data is enclosed in square brackets, indicating that it is now an array of objects.

Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author