How To Add A New Array Element To A JSON Object With JavaScript?
Maybe your like
- HTML
- CSS
- JavaScript
- Python
- Contact
- About the Site
- About the Author
Sometimes, we want to add a new array element to a JSON object with JavaScript.
In this article, we’ll look at how to add a new array element to a JSON object with JavaScript.
How to add a new array element to a JSON object with JavaScript?
To add a new array element to a JSON object with JavaScript, we parse the JSON string into an object, add the item to the array in the object, and the convert the object back to a JSON string.
For instance, we write
let jsonStr = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}'; const obj = JSON.parse(jsonStr); obj["theTeam"].push({ teamId: "4", status: "pending" }); jsonStr = JSON.stringify(obj);to call JSON.parse to parse jsonStr into an object.
Then we call push to push a new entry into the obj["theTeam"] array.
Finally we call JSON.stringify to convert obj back into a JSON string.
Conclusion
To add a new array element to a JSON object with JavaScript, we parse the JSON string into an object, add the item to the array in the object, and the convert the object back to a JSON string.
Related Posts
- How to add a new key value pair in existing JSON object using JavaScript?
Sometimes, we want to add a new key value pair in existing JSON object using…
- How to convert a JavaScript object to array?
Sometimes, we want to convert a JavaScript object to array. In this article, we'll look…
- How to create JSON object dynamically via JavaScript?
Sometimes, we want to create JSON object dynamically via JavaScript. In this article, we'll look…
By John Au-Yeung
Web developer specializing in React, Vue, and front end development.
View Archive How to check if character is number with JavaScript? How to implement regions/code collapse in JavaScript?Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Current ye@r * Leave this field emptyTag » Add Element Json Jquery
-
Adding/removing Items From A JavaScript Object With JQuery
-
Jquery Add New Node To Json Object Code Example
-
Jquery Add New Property To Json Object Code Example
-
tJSON() | JQuery API Documentation
-
rseJSON() | JQuery API Documentation
-
.data() | JQuery API Documentation
-
How To Add Data In JSON File Using Node.js ? - GeeksforGeeks
-
JQuery Add Elements - W3Schools
-
How Can I Append Json Object To
-
JQuery And JSON - Add Element
-
How To Convert HTML Form Field Values To A JSON Object
-
Adding Items To A JSON Object - JavaScript - SitePoint Forums
-
FormData Append Javascript Array (ringify), How To Cast As ...
-
Access And Print A Specific JSON Value | Documenting APIs
-
Select Values From A JSON Object Using JQuery - W3resource
-
I Want To Add A New JSON Object To The Already Existing JSON Array ...
-
Manipuler Des Données JSON - Apprendre Le Développement Web
-
Add To Array Javascript
-
Working With JSON Object With Examples - Dot Net Tutorials