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 Interview Preparation Interview Preparation For Software Developers Must Coding Questions - Company-wise Must Do Coding Questions - Topic-wise Company-wise Practice Problems Company Preparation Competitive Programming Software Design-Patterns Company-wise Interview Experience Experienced - Interview Experiences Internship - Interview Experiences Practice @Geeksforgeeks Problem of the Day Topic-wise Practice Difficulty Level - School Difficulty Level - Basic Difficulty Level - Easy Difficulty Level - Medium Difficulty Level - Hard Leaderboard !! Explore More... Data Structures Arrays Linked List Stack Queue Binary Tree Binary Search Tree Heap Hashing Graph Advance Data Structures Matrix String All Data Structures Algorithms Analysis of Algorithms Searching Algorithms Sorting Algorithms Pattern Searching Geometric Algorithms Mathematical Algorithms Randomized Algorithms Greedy Algorithms Dynamic Programming Divide & Conquer Backtracking Branch & Bound All Algorithms Programming Languages C C++ Java Python C# Go Lang SQL PHP Scala Perl Kotlin Web Technologies HTML CSS JavaScript Bootstrap Tailwind CSS AngularJS ReactJS jQuery NodeJS PHP Web Design Web Browser File Formats Computer Science Subjects Operating Systems DBMS Computer Network Computer Organization & Architecture TOC Compiler Design Digital Elec. & Logic Design Software Engineering Engineering Mathematics Data Science & ML Complete Data Science Course Data Science Tutorial Machine Learning Tutorial Deep Learning Tutorial NLP Tutorial Machine Learning Projects Data Analysis Tutorial Tutorial Library Python Tutorial Django Tutorial Pandas Tutorial Kivy Tutorial Tkinter Tutorial OpenCV Tutorial Selenium Tutorial GATE CS GATE CS Notes Gate Corner Previous Year GATE Papers Last Minute Notes (LMNs) Important Topic For GATE CS GATE Course Previous Year Paper: CS exams Like