Skip to main content

Import JSON File Using AngularJS

Screenshot
Body

In this tutorial we will create Import JSON File using AngularJS. The program will upload and display the json object as a readable text format. Feel free to use this code and modify as you learn from it. To learn more about this, just follow the steps below.

Getting started:

First you will need to download & install the AngularJS here's the link https://angularjs.org/.

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

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. <br />
  2. <!DOCTYPE html><br />
  3. <html lang="en" ng-app="myModule"><br />
  4. <head><br />
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/><br />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/><br />
  7. </head><br />
  8. <body ng-controller="myController"><br />
  9. <nav class="navbar navbar-default"><br />
  10. <div class="container-fluid"><br />
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class="col-md-3"></div><br />
  15. <div class="col-md-6 well"><br />
  16. <h3 class="text-primary">Import JSON File Using AngularJS</h3><br />
  17. <hr style="border-top:1px dotted #ccc;"/><br />
  18. <div class="col-md-3"><br />
  19. <button class="btn btn-primary" type="button" ng-click="importData()"><span class="glyphicon glyphicon-plus"></span> Import</button><br />
  20. </div><br />
  21. <div class="col-md-6"><br />
  22. <table class="table table-bordered"><br />
  23. <thead class="alert-info"><br />
  24. <tr><br />
  25. <th>Firstname</th><br />
  26. <th>Lastname</th><br />
  27. <th>Address</th><br />
  28. </tr><br />
  29. </thead><br />
  30. <tbody style="background-color:#fff;"><br />
  31. <tr ng-repeat="member in members | orderBy: 'lastname'"><br />
  32. <td>{{member.firstname}}</td><br />
  33. <td>{{member.lastname}}</td><br />
  34. <td>{{member.address}}</td><br />
  35. </tr><br />
  36. </tbody><br />
  37. </table><br />
  38. </div><br />
  39. </div><br />
  40. <script src="js/angular.js"></script><br />
  41. <script src="js/script.js"></script><br />
  42. </body><br />
  43. </html><br />
  44. </html><br />

Creating the Main Function

This code contains the main function of the application. The code will dynamically display the json data when import. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js folder as shown below.

script.js

  1. <br />
  2. var app = angular.module("myModule", [])<br />
  3. .controller("myController", function($scope){</p>
  4. <p> $scope.members = [<br />
  5. {name: "John Smith", age: "21"},<br />
  6. {name: "Claire Temple", age: "18"}<br />
  7. ];</p>
  8. <p> $scope.members2 = [];</p>
  9. <p> $scope.transfer = function(member){<br />
  10. $scope.members2.push(member);<br />
  11. $scope.members.splice($scope.members.indexOf(member), 1);</p>
  12. <p> };<br />
  13. });<br />

data.json

  1. <br />
  2. [<br />
  3. {"firstname": "John", "lastname": "Smith", "address": "New York"},<br />
  4. {"firstname": "Claire", "lastname": "Temple", "address": "Racoon City"}<br />
  5. ]<br />

There you have it we successfully created a Import JSON File 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!!

Add new comment