HTML Code to JSON Converter App Using HTML, CSS, and JavaScript Last Updated : 09 Aug, 2024 Comments Improve Suggest changes Like Article Like Report HTML tables are a common way to represent structured data on web pages, but transforming this data into a JSON format can be troublesome. JSON is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. We will be creating the HTML Code for the JSON Converter tool. The converter is designed to analyze HTML code, identify tables within it, and convert the table data into JSON format.PrerequisitesHTMLCSSJavaScriptApproachCreate an HTML structure that includes input fields for the user to enter the HTML code, a button to trigger the conversion, and an output field to display the JSON result.Apply CSS styles to create an appealing layout for the app, making it visually engaging and user-friendly.Create a function using JavaScript that converts data into JSON when the user clicks the "Convert to JSON" button.Example: In this example, we will be demonstrating a JSON Converter app which converts the data into JSON format. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>HTML to JSON Converter</title> </head> <body> <div> <h1>HTML to JSON Converter</h1> <div class="container"> <div class="input-container"> <textarea id="htmlInput" placeholder="Paste your HTML code here"> </textarea> <div> <button onclick="convertToJson()">Convert to JSON</button> <button onclick="clearFields()">Refresh</button> </div> </div> <textarea id="jsonOutput" readonly></textarea> </div> </div> <script src="script.js"></script> </body> </html> CSS body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #4caf50; } h1 { text-align: center; color: white; } .container { text-align: center; width: 1200px; justify-content: space-between; display: flex; } .input-container { margin-bottom: 20px; } textarea { width: 500px; height: 500px; padding: 10px; margin-top: 5px; } button { background-color: white; color: #4caf50; padding: 10px 20px; border: none; font-size: 18px; cursor: pointer; border-radius: 10px; border-color: #4caf50; } button:hover { font-weight: bold; } Javascript const htmlInput = document.getElementById('htmlInput'); const jsonOutput = document.getElementById('jsonOutput'); // Function to convert table data into JSON const convertToJson = () => { const htmlCode = htmlInput.value; const parser = new DOMParser(); const doc = parser.parseFromString(htmlCode, 'text/html'); const table = doc.querySelector('table'); if (table) { const jsonData = extractTableData(table); if (jsonData) { const jsonString = JSON.stringify(jsonData, null, 2); jsonOutput.innerHTML = jsonString.replace(/, /g, ',<br> '); // Formatting JSON for readability } else { jsonOutput.innerText = 'Table is missing some data.'; } } else { jsonOutput.innerText = 'No table present in the given code.'; } }; // Function to extract table data and convert it to JSON format const extractTableData = (table) => { const data = []; const headers = []; const headerCells = table.rows[0].querySelectorAll('th'); for (const cell of headerCells) { headers.push(cell.innerText.trim()); } for (let i = 1; i < table.rows.length; i++) { const tableRow = table.rows[i]; const rowData = {}; for (let j = 0; j < tableRow.cells.length; j++) { const cell = tableRow.cells[j]; rowData[headers[j]] = cell.innerText.trim(); } if (Object.values(rowData).some(value => value !== "")) { data.push(rowData); } } return headers.length === 0 || data.length === 0 ? null : data; }; // Function to clear user input and output const clearFields = () => { htmlInput.value = ''; jsonOutput.innerHTML = ''; }; Output Comment More infoAdvertise with us Next Article HTML Code to JSON Converter App Using HTML, CSS, and JavaScript R rn540 Follow Improve Article Tags : HTML Similar Reads Design a Tip Calculator using HTML, CSS and JavaScript The tip is the money given as a gift for good service to the person who serves you in a restaurant. In this project, a simple tip calculator is made that takes the billing amount, type of service, and the number of persons as input. As per the three inputs, it generates a tip for the serving person. 4 min read How to create notes taking site using HTML, Bootstrap and JavaScript ? We are going to make a website that will take our notes and saves them for our future use using HTML, CSS and JavaScript .Prerequisite:Basic understanding of HTML, Bootstrap, and JavaScript.Approach:HTML: We will create the basic framework of the website using HTML.Bootstrap: makes our work easier a 2 min read Create a User Polls Project using HTML CSS & JavaScript Creating a user polls project has various valuable purposes, including gathering feedback, engaging users, personalizing content, fostering community, conducting research, A/B testing, and improving the user experience. Clear objectives are crucial, and in this example, we focus on collecting data o 3 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 How to export HTML table to CSV using JavaScript ? Comma Separated Values or CSV is a type of text file where each value is delimited by a comma. CSV files are very useful for the import and export of data to other software applications.Sometimes while developing web applications, you may come into a scenario where you need to download a CSV file co 6 min read Dynamic HTML Using Handlebars JavaScript The Dynamic HTML Using Handlebars JavaScript consists of creating the user interactive web pages by combining the HTML with the Handlebars templating engine. The Handlebars mainly ease the process of dynamically rendering the HTML content by allowing the insertion of variables, expressions, and logi 3 min read How to fetch data from JSON file and display in HTML table using jQuery ? The task is to fetch data from the given JSON file and convert data into an HTML table. Approach: We have a JSON file containing data in the form of an array of objects. In our code, we are using jQuery to complete our task. The jQuery code uses getJSON() method to fetch the data from the file's loc 3 min read HTML Exercises, Practice Questions and Solutions Are you eager to learn HTML or looking to brush up on your skills? Dive into our HTML Exercises, designed to cater to both beginners and experienced developers. With our interactive portal, you can engage in hands-on coding challenges, track your progress, and elevate your web development expertise. 3 min read How to convert an HTML element or document into image ? This article is going to tell and guide the users to convert a div element into an image using AngularJS. The user will be generating an image from the webpage and also be able to convert a particular part of the HTML page into the picture. Also, the user needs an HTML tag and html2canvas JavaScript 3 min read How to Implement Charts from External Files using CanvasJS ? In this article, we will learn to implement bar and column charts from JSON and CSV external files using the Canvas JS plugin.Bar charts are the pictorial representation of data in groups, either in horizontal or vertical bars where the length of the bar represents the value of the data present on t 4 min read Like