File size: 1,725 Bytes
a8c95a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c990a1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JSON to HTML Converter</title>
</head>
<body>

  <h2>Paste JSON Below:</h2>
  <textarea id="jsonInput" rows="10" cols="50" placeholder="Paste JSON here"></textarea>
  <br><br>
  <button onclick="convertJson()">Convert JSON to HTML</button>

  <h2>Output:</h2>
  <div id="output"></div>

  <script>
    // Recursive function to convert JSON to HTML
    function jsonToHtml(data) {
      let html = '';

      if (Array.isArray(data)) {
        html += '<ul>';
        data.forEach(item => {
          html += `<li>${jsonToHtml(item)}</li>`;
        });
        html += '</ul>';
      } else if (typeof data === 'object' && data !== null) {
        for (const key in data) {
          if (Array.isArray(data[key]) || typeof data[key] === 'object') {
            html += `<div aria-label="${key}">${jsonToHtml(data[key])}</div>`;
          } else {
            html += `<p aria-label="${key}">${data[key]}</p>`;
          }
        }
      } else {
        html += data;
      }

      return html;
    }

    // Function to handle JSON input and display HTML
    function convertJson() {
      const jsonInput = document.getElementById("jsonInput").value;
      try {
        const jsonData = JSON.parse(jsonInput);  // Parse JSON input
        const htmlContent = jsonToHtml(jsonData);  // Convert to HTML
        document.getElementById("output").innerHTML = htmlContent;  // Display HTML
      } catch (error) {
        document.getElementById("output").innerHTML = "<p style='color: red;'>Invalid JSON. Please check the input.</p>";
      }
    }
  </script>

</body>
</html>