Read JSON Data in Javascript and jQuery
Convert JSON Data in JavaScript and jQuery
This tutorial explains how to read JSON data from various sources using JavaScript and jQuery.
1. How to read JSON data from local array.
2. How to read JSON data from external file.
3. How to read JSON data from URL.
1. How to read local JSON Array in JavaScript?
MenuTop
read-json-array-JS.html
<!DOCTYPE html> <html> <head> <title>Coding-zon Demo - Read JSON in Javascript</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <h2>How to read a json array in table format using jQuery</h2> <table style="width: 80%" border="1" id="t1"> <thead> <tr> <th>Id<</th> <th>Name</th> <th>Age</th> <th>City</th> </tr> <thead> <tbody> </tbody> </table> <script> var data = [ { id : "001", name : "Anna", age : 22, city : "London" }, { id : "002", name : "John", age : 23, city : "Paris" }, { id : "003", name : "Sofie", age : 21, city : "Texas" } ]; for(var i = 0, len = data.length; i < len; i++) { var temp = '<tr><td>' + data[i].id + '</td>'; temp+= '<td>' + data[i].name+ '</td>'; temp+= '<td>' + data[i].age + '</td>'; temp+= '<td>' + data[i].city + '</td></tr>'; $('#t1 tbody').append(temp); } </script>
</body>
</html>
Result
GoTop
The below given example takes a JSON ARRAY String that contains JSON object literals inside, So JavaScript first parses it first, and displays objects in a HTML table using jQuery.
read-json-array-string-JS.html
Example-2
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h2>How to Parse a JSON Array and Display in table format using Javascript</h2>
<table style="width: 80%" border="1" id="t1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
const txt = '[{"id": 1, "name": "Tom", "age": 23}, {"id": 2, "name": "Alice", "age": 22}, {"id": 3, "name": "John", "age": 24}, {"id": 3, "name": "Sony", "age": 21}]';
var students = JSON.parse(txt);
for(var i = 0, len = students.length; i < len; i++) {
var temp = '<tr><td>' + students[i].id + '</td>';
temp+= '<td>' + students[i].name + '</td>';
temp+= '<td>' + students[i].age + '</td></tr>';
$('#t1 tbody').append(temp);
}
</script>
</body>
</html>
Result:
2. How to read an external local JSON file in JavaScript?
Top
data.JSON
var jsondata = '{"name":"John","email" :"john@example.com","phone" : "12341234"}' ;
Linking External file
Example:
<script type="text/javascript" src="data.json"></script>
Code Example
read-external-json-js.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="data.json"></script>
</head>
<body>
<h2>Reading data from External JSON file.</h2>
<script>
var jsobj = JSON.parse(jsondata);
document.write("<br>name: "+ jsobj.name);
document.write("<br>email: "+ jsobj.email);
document.write("<br>Phone: "+ jsobj.phone);
</script>
</body>
</html>
Result:
Move toTop
3. How to read JSON data from url Using jQuery getJSON() method.
In the example, we read JSON data from https://jsonplaceholder.typicode.com/posts The returned object has three attributes: Id, Title, and Body.
GoTop
read-json-url-js.html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<h2>How to read JSON from URL using jQuery - getJSON</h2>
<table style="width: 80%" border="1">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<script>
$.getJSON('https://jsonplaceholder.typicode.com/posts', function(posts) {
for(var i = 0, len = posts.length; i < len; i++) {
var temp = '<tr><td>' + posts[i].id + '</td>';
temp+= '<td>' + posts[i].title+ '</td>';
temp+= '<td>' + posts[i].body + '</td></tr>';
$('tbody').append(temp);
});
</script>
</body>
</html>
Result:
TopMenu
Comments
Post a Comment