Skip to main content

JavaScript - Parsing JSON Object Using AngularJS


In this tutorial we will create a Parsing JSON Object Using AngularJS. This code will automatically populate the HTML table when the user click the parsing button. The code itself use a simple AngularJS directive ng-click to initiate a method that handle in retrieving of JSON file using $http.get() by providing the full file name of the JSON object. This is a free source code you can modify it and use it on your working programs.

AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, you will need to download the AngularJS here's the link https://angularjs.org/.

Note: This code will only work if run in a local server.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Parsing JSON Object Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17.  
  18. <div class="col-md-3">
  19. <label>JSON file name</label>
  20.  
  21. <center><h4><i>user.json</i></h4></center>
  22.  
  23. <br />
  24. <center><button ng-click="getData()" class="btn btn-primary btn-sm">Parse JSON Object</button></center>
  25. </div>
  26. <div class="col-md-9">
  27. <table class="table table-bordered">
  28. <thead class="alert-info">
  29. <tr>
  30. <th>Firstname</th>
  31. <th>Lastname</th>
  32. <th>Username</th>
  33. <th>Password</th>
  34. </tr>
  35. </thead>
  36. <tr ng-repeat="user in users">
  37. <td>{{user.firstname}}</td>
  38. <td>{{user.lastname}}</td>
  39. <td>{{user.username}}</td>
  40. <td>{{user.password}}</td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </div>
  45. </div>
  46. <script src="js/angular.js"></script>
  47. <script src="js/script.js"></script>
  48. </body>
  49. </html>

Creating the Script

This code contains the main function of the application. This code will display the data from a JSON file. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below.

user.json

  1. [
  2. {"firstname": "John", "lastname": "Smith", "username": "john123", "password": "john33333"},
  3. {"firstname": "Claire", "lastname": "Temple", "username": "claire", "password": "clairetemp123"}
  4. ]

script.js
Note: Save this file inside the js directory as script.js to make the program work.

  1. var app = angular.module('myModule', [])
  2. .controller('myController', function($scope, $http){
  3.  
  4.  
  5. $scope.getData = function(){
  6. $http.get('user.json').then(function(response){
  7. $scope.users = response.data;
  8. });
  9. }
  10. });

There you have it we successfully created a Parsing JSON Object Using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.