How to Update JSON Object Value Dynamically in jQuery? Last Updated : 17 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In jQuery, we can update JSON object values dynamically by manipulating the object directly or using utility functions. We will explore two approaches to update JSON object values dynamically in jQuery. Below are the approaches to update JSON object values dynamically in jQuery. Table of Content Using each() MethodUsing hasOwnProperty() methodUsing each() MethodIn this approach, we use each() method in jQuery to iterate through the keys of the JSON object. When the input key matches an existing key in the object, we dynamically update its corresponding value and display the updated JSON object. Example: The example below uses each() Method to update the JSON object value in jQuery dynamically. HTML <!DOCTYPE html> <html> <head> <title>each() Method</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"> </script> <style> body { font-family: Arial, sans-serif; margin: 20px; } label { display: block; margin-bottom: 10px; } input[type="text"] { width: 30%; padding: 8px; margin-bottom: 15px; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; } #output { border: 1px solid #ccc; width: 50%; padding: 10px; border-radius: 5px; margin-bottom: 15px; } #updateBtn { padding: 10px 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; } #updateBtn:hover { background-color: #0056b3; } </style> </head> <body> <h1>Using each() Method</h1> <div> <label for="keyInput">Enter Key:</label> <input type="text" id="keyInput"> <label for="valueInput">Enter Value:</label> <input type="text" id="valueInput"> <button id="updateBtn">Update Value</button> </div> <div id="output"></div> <script> let jsonObject = { "JavaScript": "Programming Language", "PHP": "Server Side Language" }; $("#output").text(JSON.stringify(jsonObject)); $("#updateBtn").click(function () { const key = $("#keyInput").val(); const value = $("#valueInput").val(); $.each(jsonObject, function (objKey, objValue) { if (objKey === key) { jsonObject[objKey] = value; } }); $("#output").text(JSON.stringify(jsonObject)); $("#keyInput").val(''); $("#valueInput").val(''); }); </script> </body> </html> Output: Using hasOwnProperty() MethodIn this approach, we use the hasOwnProperty() method in JavaScript to check if a key exists in the JSON object before updating its value dynamically in jQuery. If the key exists, the value is updated otherwise an error alert is shown. Example: The below example uses hasOwnProperty() to update the Json object value dynamically in jQuery. HTML <!DOCTYPE html> <html> <head> <title>Using hasOwnProperty() method</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> body { font-family: Arial, sans-serif; margin: 20px; } label { display: block; margin-bottom: 10px; } input[type="text"] { width: 30%; padding: 8px; margin-bottom: 15px; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; } #output { border: 1px solid #ccc; width: 50%; padding: 10px; border-radius: 5px; margin-bottom: 15px; } #updateBtn { padding: 10px 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; } #updateBtn:hover { background-color: #0056b3; } </style> </head> <body> <h1>Using hasOwnProperty() method</h1> <div> <label for="updatesInput"> Enter Updates: </label> <input type="text" id="updatesInput" placeholder="key1:value1,key2:value2,..."> <button id="updateBtn"> Update Values </button> </div> <div id="output"></div> <script> let jsonObject = { "JavaScript": "Programming Language", "PHP": "Server Side Language" }; $("#output").text(JSON.stringify(jsonObject)); $("#updateBtn").click(function () { const updatesInput = $("#updatesInput").val(); const updates = updatesInput.split(",") .reduce(function (obj, update) { const parts = update.split(":"); const key = parts[0]; const value = parts[1]; if (jsonObject.hasOwnProperty(key)) { jsonObject[key] = value; } else { alert("Error: Key '" + key + "not found in the object."); } return obj; }, {}); $("#output").text(JSON.stringify(jsonObject)); $("#updatesInput").val(''); }); </script> </body> </html> Output: Comment More infoAdvertise with us G gauravggeeksforgeeks Follow Improve Article Tags : Web Technologies JQuery JSON Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like