Jump to content
Sign in to follow this  
smus

AngularJS inserting(updating,deleting) data into(from) a JSON file

Recommended Posts

I have 3 files: html that is displaying data from json file using AngularJS, json file containing the data, script.js that stores the scripts.

HTML file also has a form, from which I want to add data to my json file (onsubmit="insertData()"). insertData function is going to be in script.js

I know, I may use .push method for this, but I don't know how to apply to the json file in this case.

script.js

var prg = angular.module('ajsprogram', []);   

prg.controller("outercont", function($scope, $http) {      //function to display json file information
        $http.get('airlines.json').success(function(data){
            $scope.info = data;
            $scope.airlineorder = 'airline';
        });
});

function newairline(an,hb,ur,cs){                         //new function to insert a data
  
}

airlines.json

[
        {
           "airline" : "British Airways",
           "hub"     : "LHR",
           "url"       : "http://britishairways.com",
           "callsign":"SPEEDBIRD"     
        },
        {
           "airline" : "Air France",
           "hub"     : "CDG",
           "url"       : "http://airfrance.com",       
           "callsign":"AIRFRANS"        
         },
        {
           "airline" : "Virgin Atlantic",
           "hub"     : "LGW",
           "url"       : "http://virginatlantic.com",
          "callsign":"VIRGIN"       
        },
        {
           "airline"   : "RyanAir",
           "hub"       : "DUB",
           "url"         : "http://ryanair.com",  
           "callsign":"RYANAIR"     
        },
        {
           "airline" : "Emirates",
           "hub"     : "DXB",
           "url"       : "http://emirates.com",
           "callsign":"EMIRATES"      
        }
]

 

 

Edited by smus

Share this post


Link to post
Share on other sites

If you want to write to a file on the server then you will need a server-side program that does the writing. Javascript is not able to write to the server.

Share this post


Link to post
Share on other sites
10 hours ago, Ingolme said:

If you want to write to a file on the server then you will need a server-side program that does the writing. Javascript is not able to write to the server.

I know, but I have asked the same question on StackOverflow. Can you please consider their solution and advise me if there is a way to somehow use it in separate files?

Here is the link: https://stackoverflow.com/questions/50590055/angularjs-inserting-data-to-a-json-file/50596513#50596513

Share this post


Link to post
Share on other sites

The answer given on StackOverflow will update the data structure, but it will not be saved to a file. The data disappears as soon as the user leaves the page.

There is no way for Javascript to write to a file. You have to use a server-side program to write to the file for you. Javascript can send the data to the server, but the server has to write the data to the file.

  • Like 1

Share this post


Link to post
Share on other sites

May I refer to this topic, if an employer asks me to manipulate data from json file using javascript? Because it is already the second time they want me to do "something impossible" :)

Share this post


Link to post
Share on other sites

This is a public forum, you can show it to anybody.

The task is impossible using only Javascript, but do you have access to a server-side programming language?

  • Like 1

Share this post


Link to post
Share on other sites

No, those were just interview tasks with strict condition to use JS or one of its frameworks. But I decided to show that data from a form can, indeed, be added using push() method for arrays, if the json structure is inside html file. It works well, however, of course, the json data is not changing physically.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×