How to Use jq to Add New Objects into a JSON String?

Introduction In this article, we will explore how to use jq, a powerful command-line JSON processor, to add new objects into a given JSON string. Specifically, we will look at a JSON object that contains two keys, hello and world, each having nested objects. Our goal is to combine these objects and generate an updated JSON string that includes a new key called combined. Understanding the JSON Structure The JSON string we will work with is structured as follows: { "hello": { "foo": { "version": "1.1.1" } }, "world": { "bar": { "version": "2.2.2" } } } This structure contains two distinct paths in the JSON: hello and world, each leading to a version number encapsulated within nested objects. We aim to create a new object (combined) that consolidates the values under both hello and world. Step-by-Step Solution Using JQ 1. Define the JSON String First, we will define the JSON string in a Bash script. We will use a here document to make it easier to manage: json_string="$(cat

May 14, 2025 - 01:58
 0
How to Use jq to Add New Objects into a JSON String?

Introduction

In this article, we will explore how to use jq, a powerful command-line JSON processor, to add new objects into a given JSON string. Specifically, we will look at a JSON object that contains two keys, hello and world, each having nested objects. Our goal is to combine these objects and generate an updated JSON string that includes a new key called combined.

Understanding the JSON Structure

The JSON string we will work with is structured as follows:

{
  "hello": {
    "foo": {
      "version": "1.1.1"
    }
  },
  "world": {
    "bar": {
      "version": "2.2.2"
    }
  }
}

This structure contains two distinct paths in the JSON: hello and world, each leading to a version number encapsulated within nested objects. We aim to create a new object (combined) that consolidates the values under both hello and world.

Step-by-Step Solution Using JQ

1. Define the JSON String

First, we will define the JSON string in a Bash script. We will use a here document to make it easier to manage:

json_string="$(cat <<'END'
{
  "hello": {
    "foo": {
      "version": "1.1.1"
    }
  },
  "world": {
    "bar": {
      "version": "2.2.2"
    }
  }
}
END
)"

This command will read the JSON data and store it in the variable json_string.

2. Extract Existing Data

Next, we can extract the nested objects using jq. The goal here is to extract the hello and world objects:

hello=$(jq -r ".hello" <<< "$json_string")
world=$(jq -r ".world" <<< "$json_string")

After executing this, the variables hello and world will contain their respective JSON objects:

  • hello will have:
{
  "foo": {
    "version": "1.1.1"
  }
}
  • world will have:
{
  "bar": {
    "version": "2.2.2"
  }
}

3. Combining Data into Full JSON

Now, we will create the full JSON structure that includes the newly combined data. We will add a new key combined that merges the objects extracted earlier. We can achieve this with the following jq command:

full=$(jq -n --argjson hello "$hello" --argjson world "$world" '{hello: $hello, world: $world, combined: {foo: $hello.foo, bar: $world.bar}}')

This command uses jq -n to create a new JSON object and stores it in the variable full. The combined data captures the values from hello and world correctly under the new combined key.

4. Displaying the Full JSON

Finally, we can display the full JSON string by echoing the variable:

echo "$full"

This results in the expected output:

{
  "hello": {
    "foo": {
      "version": "1.1.1"
    }
  },
  "world": {
    "bar": {
      "version": "2.2.2"
    }
  },
  "combined": {
    "foo": {
      "version": "1.1.1"
    },
    "bar": {
      "version": "2.2.2"
    }
  }
}

Frequently Asked Questions

What is jq?

jq is a lightweight and flexible command-line JSON processor that allows for various manipulations and querying of JSON data.

How can I install jq?

You can install jq on most Unix-based systems using package managers, e.g., apt-get install jq for Ubuntu/Debian or brew install jq for macOS.

Can jq convert JSON output to other formats?

Yes, jq can format JSON output in a readable manner or filter data to different structures, making it a versatile tool in data processing.

Conclusion

In this tutorial, we covered the process of using jq to combine JSON objects and add new ones to an existing JSON string easily. By extracting and combining data efficiently, you can modify JSON structures for various applications. Experiment with these commands to suit your own JSON manipulation needs!