Get data from json file

 $.getJSON("json_data.json", function (data) {
            alert(data.name);
        });

in the data i am getting the whole instead can i get only data.name in the first step instead so my data will be secure like below…

 $.getJSON("json_data.json", function (data.name) {
            alert(data.name);
        });

but it is not working so there is any way to do that.

Yes, you can do a destructuring assignment, which also works for function parameters:

$.getJSON('json_data.json', function({ name }) {
  console.log(name)
})

Note that this is ES syntax though, so it won’t work on IE. :-/ You’ll have to use a transpiler such as babel for full x-browser compatibility.

Are you wanting only the name to be returned by the getJSON call?

If that is the case then you will need to change the data returned in ‘json_data.json’, as whilst the destructing assignment method works the full data within the JSON file is still downloaded.

1 Like

i got it, thanks guys.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.