Retrieving data from a json file into a variable

Retrieving data from a json file into a variable

Hi guys,

I have these two codes. One give me an array of usernames from a json file. The other one check if a username has been taken (but just from the variable "userList").

How can I implement them together? I want the second code to check inside the json file instead of the variable userList.

Also,is it possible to check inside a mongodb database instead of the json file? I have been looking every where but can't find any recent information about it.

Thank you.


  1. $.getJSON("/usernames.json", function(data) {
  2.   
  3.   data.forEach(function(item) {
  4.     console.log(item.username);
  5.   });

  6. });

  7. /*******************************************************************************/
  8. var userList = ['admin','vivek','raul','juan'];
  9.   
  10. function validateUserList() {
  11.       return $.inArray($('#username').val(), userList) > -1
  12.      
  13. }
  14.    
  15. $(function() {
  16.       $(':input[name=username]').blur(function() {
  17.         
  18.             if (!validateUserList()) {
  19.                              
  20.                   $('div#validUser').text('');
  21.             } else {
  22.                                
  23.                   $('div#validUser').text('Username is already taken.');
  24.                 }
  25.         
  26.       });
  27. });