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 Array Javascript
-
Adding A New Array Element To A JSON Object - Stack Overflow
-
How To Add JSON Object To Existing JSON Array In JavaScript | Code
-
How To Add Elements In JSON Array Using JavaScript - CodeSpeedy
-
Using Array Of JSON Objects In JavaScript - Delft Stack
-
Add Json Object To Json Array Javascript Code Example - Code Grepper
-
JSON Array Literals - W3Schools
-
How To Add An Array Element To JSON Object In JavaScript.
-
I Want To Add A New JSON Object To The Already Existing JSON Array ...
-
Add New Element To Existing Json Object - MaxInterview
-
How To Add Key And Value From Json Array Object In Javascript
-
How To Add Data In JSON File Using Node.js ? - GeeksforGeeks
-
JavaScript | How To Add An Element To A JSON Object?
-
How To Add Value In Json Array Javascript?
-
Adding A New Key Value Pair In The JSON Array [duplicate]
-
Add To Array Javascript
-
How To Add Element In Json Object Code Example
-
JSON Stringify Example – How To Parse A JSON Object With JS
-
JSON ForEach - Looping Over A JSON Array In JavaScript - ZetCode
-
ringify() - JavaScript - MDN Web Docs