Show json format in html
For the syntax highlighting, use code prettify. I believe this is what StackOverflow uses for its code highlighting. Show
You will have syntax highlighted JSON in the format you have laid out in your page. See here for an example. So if you had a code block like this:
It would look like this:
This doesn't help with the indenting, but the other answers seem to be addressing that. In this tutorial, I will show you how to fetch and display data from a JSON file using vanilla JavaScript. So how will we achieve this? First, we will fetch the JSON data by using the fetch API. This will return a promise with our JSON data. Then we will append the data dynamically by creating HTML elements on the fly. We will then append our JSON data to those elements. Getting JSON data from an API and display it on a web page is a common thing you will do quite often. I have created similar posts on the big frameworks like React, Vue and Angular. Check it out if you are using any of those frameworks. Let’s get started! First, create a [ { "id": "1", "firstName": "John", "lastName": "Doe" }, { "id": "2", "firstName": "Mary", "lastName": "Peterson" }, { "id": "3", "firstName": "George", "lastName": "Hansen" } ] We will save this file in the same directory as our Fetching the JSON dataTo be able to display this data in our HTML file, we first need to fetch the data with JavaScript. We will fetch this data by using the fetch API. We use the fetch(url) .then(function (response) { // The JSON data will arrive here }) .catch(function (err) { // If an error occured, you will catch it here }); The The If anything goes wrong (like the JSON file cannot be found), the Let us see how this will look in out example: fetch('people.json') .then(function (response) { return response.json(); }) .then(function (data) { appendData(data); }) .catch(function (err) { console.log(err); }); Here we are fetching our
The Now we can take this data and display it on our HTML
page. Notice that we are calling a function called Notice that in our Displaying the JSON dataBefore we display our JSON data on the webpage,
let’s just see how the body of our Pretty simple right? We have just a simple div with the id There are several ways to display the data in our HTML. We could create a table and make it look really good with nice styling. However, we will do this in a simple and ugly way. Our goal is to just simply display the full name of the people in our JSON file. Step 1 – Get the div element from the bodyRemember the div with the This is how we will do it: var mainContainer = document.getElementById("myData"); We get the element by executing the Step 2 – Loop through every object in our JSON objectNext step is to create a simple loop. We can then get every object in our list of JSON object and append it into our main div. for (var i = 0; i < data.length; i++) { // append each person to our page } Step 3 – Append each person to our HTML pageInside the for-loop we will append each person inside its own div. This code will be repeated three times for each person. First, we will create a new div element: var div = document.createElement("div"); Next we will fill that div with the full name from our JSON file. div.innerHTML = 'Name: ' + data[i].firstName + ' ' + data[i].lastName; Lastly, we will append this div to our main container: mainContainer.appendChild(div); That’s it. Now we have finished appending the JSON data to our index.html page. The
full function appendData(data) { var mainContainer = document.getElementById("myData"); for (var i = 0; i < data.length; i++) { var div = document.createElement("div"); div.innerHTML = 'Name: ' + data[i].firstName + ' ' + data[i].lastName; mainContainer.appendChild(div); } } When we run our index.html page, it will look something like this: Not the most beautiful application, but it got the job done. Let us look at the entire HTML file with the JavaScript:
Try to copy and paste this in your own editor. As an exercise, you can try to style the output to look nicer. Remember to include the Why use Vanilla JavaScript?You might be wondering what is the point of creating this in vanilla JavaScript. Doesn’t modern web application use frameworks and libraries like Angular, ReactJS or VueJS? Well, yeah, you are probably right, most of the time. But some web pages are just static with very little logic. If you just want to tweak some minor parts of the website, it might be overkill to include big libraries which will slow down the site. Besides, frameworks and libraries come and go. Good old vanilla JavaScript is here to stay. So take every opportunity to learn it, you don’t know when you might need it. Happy coding! How do I display a JSON table in HTML?Approach 1:. Take the JSON Object in a variable.. Call a function which first adds the column names to the < table > element. (It is looking for the all columns, which is UNION of the column names).. Traverse the JSON data and match key with the column name. ... . Leave the column empty if there is no value of that key.. How show JSON data in HTML DIV?How to display JSON data in a div when JSON data is in Array? Answer: You can use for it to loop thru the array and construct an HTML string. Use jQuery 's . append() to add the string to the body.
Can I put JSON in HTML?JSON can very easily be translated into JavaScript. JavaScript can be used to make HTML in your web pages.
How do I display JSON data in my browser?Right click on JSON file, select open, navigate to program you want open with(notepad). Consecutive opens automatically use notepad.
|