1. Introduction

The JavaScript Object Notation (JSON) format has been around since 2006, as an IETF Information Standard. It has become ubiquitous in various use cases like config files, data interchange protocols, and others.

In this tutorial, we’ll explore various ways to add an extra field to an existing JSON object. First, we’ll create a sample JSON object. Subsequently, we’ll use various tools to add an additional field to the JSON object.

We’ll use a Ubuntu 22.04 environment for running the examples.

2. Sample JSON Files

First, we’ll create a sample JSON file:

$ echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.' > fruits.json
$ cat fruits.json
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2
  }
}

As illustrated above, we use the echo command to pipe a JSON object to jq. In turn, the jq command orders and prints the JSON object to a file. We’ll use the above sample JSON file when adding key-value pairs and other objects.

In particular, we’ll explore two cases using each of the tools:

  • add an attribute to the fruit object, i.e., “source”: “India”
  • add a new object to the JSON file, i.e., “vegetable”: { “name”: “brinjal” }

Now, let’s go into examples of various tools.

3. bash and sed

Bash supports a scripting language that can be used to automate tasks, while sed is a stream editor that can be used to search and replace text. These tools come with most major Linux distributions.

Using sed along with Bash, we can modify the JSON object in a script:

$ cat add_field.sh
#!/bin/bash 
# Get the JSON object from the file
json_object=$(cat $1) 
# Create a new JSON object with the new field
new_json_object=$(echo "$json_object" | sed "s/ }/, \"$2\": \"$3\"}/") 
# Write the new JSON object
echo "$new_json_object" | jq .
$ bash add_field.sh fruits.json source India
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2,
    "source": "India"
  }
}

As shown above, the Bash script uses sed to replace the specific occurrence of space plus curly bracket (“ *}*“) with the new field within the supplied file. It does so in a subshell. However, the approach assumes the occurrence of a specific expression at a precise location.

Critically, this assumption might be wrong and the operation can fail if the pattern appears elsewhere in the file. Thus, this approach of using regular expressions in sed isn’t as versatile as the other approaches we’ll discuss.

4. jq

The jq command is a lightweight and flexible command-line JSON processor. It can be used to slice, filter, and transform the components of a JSON file. jq is written in the C programming language and is completely portable to other systems, with no runtime dependencies. In general, the jq command is a very concise way of adding an object to an existing JSON object.

4.1. Add Key-Value Pair

Let’s see a basic jq command to add an attribute to our JSON object:

$ cat fruits.json | jq '.fruit += {"source" : "India" }'
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2,
    "source": "India"
  }
}

The jq command adds a new attribute to the fruit object as requested via the += operator and the .fruit specifier.

4.2. Add an Object

Similarly, we can insert a JSON object:

$ cat fruits.json | jq '. += {"vegetable" : { "name" : "brinjal" }}'
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2
  },
  "vegetable": {
    "name": "brinjal"
  }
}

The jq command adds a new JSON object to fruits.json. In essence, we’re using two operators from the jq language. The first operator, . period, refers to the current object. The += operator is a concatenation function. To emphasize, in this example, the current object is the root JSON object.

5. Python

Python is an interpreted language and one of the most widely used languages in system administration. So, let’s use the python interpreter to add new attributes to the JSON object.

5.1. Add Key-Value Pair

Let’s see an example Python program for adding a field:

$ cat add_field.py
#!/usr/bin/env python
import sys
import json
with open(sys.argv[1]) as f:
  jobj = json.loads(f.read())
  jobj["fruit"][sys.argv[2]] = sys.argv[3]
  print(json.dumps(jobj, indent = 4))
$ python add_field.py fruits.json "source" "India" 
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2,
    "source": "India"
  }
}

The program performs several operations:

  • read the file fruits.json supplied as an argument
  • convert the file contents to a dictionary using the json.loads function
  • read the sys.argv parameters on the command-line to extract the key-value pairs
  • add the specified key-value pair via the dictionary
  • print the dictionary as a JSON string using the json.dumps function

In this example, we’re adding the new attribute to the fruit object, which is a dictionary entry. Within Python, all JSON objects are represented as nested dictionaries. In summary, we’re using the dictionary methods in Python to manipulate the JSON object.

5.2. Add an Object

Next, we add a JSON object as a value. To do so, we slightly change the code from above:

$ cat add_obj.py
#!/usr/bin/env python
import sys
import json
with open(sys.argv[1]) as f:
  jobj = json.loads(f.read())
  jobj[sys.argv[2]] = json.loads(sys.argv[3])
print(json.dumps(jobj, indent = 4))
$ python add_obj.py fruits.json "vegetable" '{ "name" : "brinjal" }'
{ 
  "fruit": { 
    "name": "apple",
    "color": "green",
    "price": 1.2
  },
  "vegetable": {
    "name": "brinjal"
  }
}

As shown above, the program creates an object using the third parameter on the command line, i.e., { “name”: “brinjal” }. Another key point in this example is that the key-value pair is an object which is added to the root JSON object. We can also add the object at any level based on the requirement.

6. Perl

Perl is a high-level, general-purpose programming language that can be used for a wide variety of tasks, including text processing, system administration, and web development. Let’s see how to use the perl interpreter for our needs.

6.1. Add Key-Value Pair

The Perl program adds a new attribute to the JSON object:

$ cat add_field.pl
#!/usr/bin/env perl
use JSON;
open(FH, $ARGV[0]) or die "Sorry!! couldn't open";
# Reading the file till FH reaches EOF 
$json = "";
while(<FH>)
{
  chomp($_);
  $json = $json.$_; 
}
close FH;
$obj_ref = decode_json($json);
$obj_ref->{"fruit"}->{$ARGV[1]} = $ARGV[2];
$json_text = encode_json ($obj_ref );
print $json_text."\n";
$ perl add_field.pl fruits.json source India
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2,
    "source": "India"
  }
}

The program uses the JSON module with its encode_json and decode_json methods to transform the objects. Thus, after reading the file, it adds the new attribute to the fruit object, using the object reference syntax.

6.2. Add an Object

Likewise, we can use a Perl script to add a new object to an existing JSON object:

$ cat add_obj.pl
#!/usr/bin/env perl
use JSON;
open(FH, $ARGV[0]) or die "Sorry!! couldn't open";
# Reading the file till FH reaches EOF
$json = "";
while(<FH>)
{ 
  chomp($_);
  $json = $json.$_;
} 
close FH;
$obj_ref = decode_json($json);
$obj_ref->{$ARGV[1]} = decode_json($ARGV[2]);
$json_text = encode_json ($obj_ref );
print $json_text."\n";
$ perl add_obj.pl fruits.json "vegetable" '{ "name" : "brinjal" }'
{ 
  "fruit": { 
    "name": "apple",
    "color": "green",
    "price": 1.2
  },
  "vegetable": {
    "name": "brinjal"
  }
}

Similar to the previous example, the program creates an object using the third parameter, i.e., { “name”: “brinjal” }. In Perl, we decode JSON objects as hashes and store them.** In summary, hashes in Perl are similar to dictionaries in Python.

7. Node.js

Node.js is an engine that allows developers to write backend applications in JavaScript via the node interpreter. Significantly, it has become one of the most widely used languages for backend server applications due to its excellent responsiveness and performance. In fact, Node.js is based on an event-driven, non-blocking I/O model.

To demonstrate, we’ll use the Node.js packages to manipulate our JSON objects.

7.1. Add Key-Value Pair

First, let’s see and run an example Node script for adding an attribute:

$ cat add_field.js
#!/usr/bin/env node
const fs = require("fs");
// Storing the JSON format data in jobj
var data = fs.readFileSync(process.argv[2]);
var jobj = JSON.parse(data);
// Adding the new field to our object
jobj['fruit'][process.argv[3]] = process.argv[4];
// Using stringify to pretty print the JSON object
console.log(JSON.stringify(jobj, null, 4));
$ node add_field.js fruits.json "source" "India"
{
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2,
    "source": "India"
  }
}

The program performs several steps:

  • read the file fruits.json using the readFileSync function from the fs module
  • convert the file contents to a JSON object using the built-in JSON.parse function
  • add the key-value pair specified on the command line
  • output the JSON object to the console via the built-in JSON.stringify

Notably, the JSON handling is built into Node.js due to its core – JavaScript. In this example, we’re adding a new attribute to the fruit object. Moreover, JSON objects are native to JavaScript.

7.2. Add an Object

Next, the following program adds a new JSON object to our existing one:

$ cat add_obj.js
#!/usr/bin/env node 
const fs = require("fs");
// Storing the JSON format data in jobj
var data = fs.readFileSync(process.argv[2]);
var jobj = JSON.parse(data);
// Adding the new field to our object
jobj[process.argv[3]] = JSON.parse(process.argv[4]);
// Using stringify to pretty print the JSON object
console.log(JSON.stringify(jobj, null, 4));
$ add_obj.js fruits.json "vegetable" '{ "name" : "brinjal" }'
{ 
  "fruit": {
    "name": "apple",
    "color": "green",
    "price": 1.2
  },
  "vegetable": {
    "name": "brinjal"
  }
}

Similar to our other examples, we use the third command-line parameter to specify the object to add*.* First, we read the whole file and convert it to a JSON object via JSON.parse. After that, we use the same function to also parse the third parameter as a JSON object. Next, we add the object to our base object by manipulating the object directly. Finally, we use the JSON.stringify function to pretty print the JSON object.

8. Conclusion

In this article, we learned a few ways to add an attribute and an object to an existing JSON object.

Firstly, we created a fairly limited Bash program that relies on sed and a regular expression*.* Secondly, using the jq command is perhaps the simplest and most concise way of manipulating the JSON object from the command line. Lastly, Python, Perl, and Node scripts aided our goal, but may require deeper knowledge to use and expand upon.