How to Convert HTML Form Field Values to JSON Object using JavaScript? Last Updated : 18 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Storing HTML input data into JSON format using JavaScript can be useful in various web development scenarios such as form submissions, data processing, and AJAX requests. Here we will explore how to convert the HTML form input data into the JSON format using JavaScript. ApproachThe HTML file contains a form with input fields for name, email, and message, and a submit button.Upon clicking the submit button, the convertToJson() function is called, defined in an external JavaScript file.Inside convertToJson(), it selects the form element and initializes an empty object formData to store input data.It iterates over each element in the form using a for loop, excluding the submit button and adds key-value pairs to formData where the key is the element's name attribute and the value is its value attribute.After converting formData to a JSON string using JSON.stringify(), it updates the content of the jsonOutput div with the JSON string wrapped in <pre> tags for better readability.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>The HTML Input to JSON</title> </head> <body> <form id="dataForm"> <label for="name"> Name: </label> <input type="text" id="name" name="name"><br><br> <label for="email"> Email: </label> <input type="email" id="email" name="email"><br><br> <label for="message"> Message: </label><br> <textarea id="message" name="message" rows="4" cols="50"> </textarea><br><br> <button type="button" onclick="convertToJson()"> Submit </button> </form> <div id="jsonOutput"></div> <script src="script.js"></script> </body> </html> JavaScript function convertToJson() { let form = document.getElementById("dataForm"); let formData = {}; for (let i = 0; i < form.elements.length; i++) { let element = form.elements[i]; if (element.type !== "submit") { formData[element.name] = element.value; } } let jsonData = JSON.stringify(formData); let jsonOutput = document.getElementById("jsonOutput"); jsonOutput.innerHTML = "<pre>" + jsonData + "</pre>"; } Output: Comment More infoAdvertise with us Next Article How to Convert HTML Form Field Values to JSON Object using JavaScript? V vikas2gqb5 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to convert JSON data to a html table using JavaScript/jQuery ? To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a v 4 min read How to convert form data to JavaScript object with jQuery ? JavaScript objects are complex data types that can store multiple values and properties. Unlike primitive data types that only hold a single value, objects can hold various data types, including other objects, arrays, functions, and more. The inputs given by a user can be converted into objects wher 3 min read How to Convert JSON Object to CSV in JavaScript ? JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte 3 min read How to Convert HTML Table to JSON in JavaScript? HTML tables are commonly used to present structured data on websites. In many scenarios, this tabular data needs to be converted to JSON format for processing, storage, or server communication. We will discuss different approaches to converting HTML tables to JSON using JavaScript.These are the foll 3 min read How to Convert a JavaScript Object to a Form Data in JavaScript? In web development, especially when dealing with forms and AJAX requests, you might find yourself needing to convert a JavaScript object into a FormData object. The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can the 4 min read How to Convert HTML to JSON in JavaScript ? Converting HTML to JSON is important for structured data extraction and integration with JavaScript applications. Here, we will learn different approaches to converting HTML to JSON in JavaScript. Below are the approaches to convert html to JSON in JavaScript: Table of Content Using html-to-json Lib 2 min read How to Create an HTML Table from an Object Array Using JavaScript ? Tables are a fundamental part of web development, and displaying data in a structured manner is a common requirement. JavaScript provides a powerful way to dynamically generate HTML content, making it easy to create tables from object arrays. Table of Content Using innerHTML propertyUsing appendChil 2 min read How to convert an object to string using JavaScript ? To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp 4 min read How to select values from a JSON object using jQuery ? In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. This append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).appen 2 min read How to Convert Excel to JSON in JavaScript ? Converting Excel spreadsheets to JSON format is a common requirement in various applications. JavaScript code utilizes the read-excel-file library to parse the Excel data, convert it to JSON format, and display it. Additionally, it provides functionality to download the generated JSON file. Approach 3 min read Like