Add new key value to a json data in javascript

A simple one!

In JavaScript, you can add a new key-value pair to a JSON object using the following methods:

Method 1: Using the dot notation

var jsonData = {
  "name": "John",
  "age": 30
};

jsonData.newKey = "newValue"; // adds a new key-value pair

console.log(jsonData); // Output: { "name": "John", "age": 30, "newKey": "newValue" }

Method 2: Using the bracket notation

var jsonData = {
  "name": "John",
  "age": 30
};

jsonData["newKey"] = "newValue"; // adds a new key-value pair

console.log(jsonData); // Output: { "name": "John", "age": 30, "newKey": "newValue" }

Method 3: Using the JSON.parse() and JSON.stringify() methods

var jsonData = '{"name": "John", "age": 30}';
var jsonObject = JSON.parse(jsonData);
jsonObject.newKey = "newValue";
var jsonString = JSON.stringify(jsonObject);

console.log(jsonString); // Output: {"name": "John", "age": 30, "newKey": "newValue"}

Note that in all cases, the new key-value pair is added to the existing JSON object. If you want to create a new JSON object with the new key-value pair, you can use the JSON.parse() and JSON.stringify() methods as shown in Method 3.

Also, if you're working with a JSON string, you can use the JSON.parse() method to convert it to a JSON object, and then add the new key-value pair using one of the above methods.