Friday, September 5, 2025
HomeLanguagesJavascriptJSON Modify an Array Value of a JSON Object

JSON Modify an Array Value of a JSON Object

The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types:

The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modified. It can be simply done by modifying the value present at a given index. 

Example: Modifying the value present at an index in the array 

Javascript




let myObj, i;
 
myObj = {
 
    // Stored the values
    "words": ["I", "am", "Good"]
};
 
// Modifying the value present at index 2
myObj.words[2] = "bad";
 
for (i in myObj.words) {
 
    // Displaying the modified content
    console.log(myObj.words[i]);
}


Output

I
am
bad

Note: If the value is modified at an index that is out of the array size, then the new modification will not replace anything in the original information but rather will be an add-on. 

Example: Modifying the value of the index which is out of the array size. 

Javascript




let myObj, i;
 
myObj = {
 
    // Stored the values
    "words": ["I", "am", "Good"]
};
 
// Modifying the value present at index 2
myObj.words[3] = "bad";
 
for (i in myObj.words) {
 
    // Displaying the modified content
    console.log(myObj.words[i]);
}


Output

I
am
Good
bad
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS