Narsimhulu
Narsimhulu

Reputation: 77

Get Exact value by key or id from json file using JQuery

I have a json file containing with 50,000 user details. i need to display the user details like name , mobile and address by using user id. am done this with below code successfully but, it's taking too much time (more than 15 seconds) to get data and display on a html page.

so, please help me to reduce the time consumption to fetch data from json file.

html code is

<input type="text" id="search">
<input type="submit" id="submit">

i have json file (convertcsv.json) with below details

[
 {
   "id": 22313,
   "Name": "BABU",
   "Address": "Hyderabad"
 },
 {
  "id": 21314,
  "Name": "Raju",
  "Address": "BENGALURU"
 }
]

the actual JQuery script is

<script>
  $('#submit').on('click', function(){
    var searchField = $('#search').val();
    var regex = new RegExp(searchField, "i");
    var output = '<div class="row">';
    var count =0;
    $.getJSON('convertcsv.json', function(data) {
      $.each(data, function(key, val){
        if (val.id==searchField ) {
          output += '<div class="dataContain">';
          output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
          output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
          output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
          output += '</div>';             
          count++;
        }


      });
     /// alert(count);
if(count==0){
output += 'No Records Found'
}
//alert(output)
      output += '</div>';
      //$('#results').toggle();
      $('#results').html(output);
    });
});

then the result is displayed in tag

<div id="results"> Result Displayed here, i need to improve time, please help me, thanks in advance</div> 

Thank you.

Upvotes: 1

Views: 2659

Answers (2)

Narsimhulu
Narsimhulu

Reputation: 77

thank you very much @Ali Shahbaz , the final answer is.,

var jsonUsers;
$(document).ready(function () {
   $.getJSON('convertcsv.json', function(data) {
     jsonUsers = data;
   });
});


  // filter results given searchField then loop through from resulted JSON array
  // filter returns an array

  let result = jsonUsers.filter(x=> x.id == searchField);
  $.each(result, function(key, val){

      output += '<div class="dataContain">';
      output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
      output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
      output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
      output += '</div>';             
      count++;

    });

now it's working very well, without consuming loading time.

Upvotes: 1

Ali Shahbaz
Ali Shahbaz

Reputation: 845

var json = [
 {
   "id": 22313,
   "Name": "BABU",
   "Address": "Hyderabad"
 },
 {
  "id": 21314,
  "Name": "Raju",
  "Address": "BENGALURU"
 }
]

console.log(json.filter(x=> x.id == '22313'))

Load your JSON file and then filter like above example, then create HTML from resulted JSON array.

EDIT 1

$.getJSON('convertcsv.json', function(data) {

      // filter results given searchField then loop through from resulted JSON array
      // filter returns an array

      let result = data.filter(x=> x.id == searchField);
      $.each(result, function(key, val){

          output += '<div class="dataContain">';
          output += '<p><span style="font-weight:bold;"> id: </span>'+val.id+ '</p>';
          output += '<p><span style="font-weight:bold;"> Name: </span>'+val.Name + '</p>';
          output+='<p><span style="font-weight:bold;">Address: </span><span class="status">' + val.Address+'<span class="circle"></span></span></p>';
          output += '</div>';             
          count++;

        });


      });

EDIT 2

Load JSON when document is ready and store into variable.

var jsonUsers;
$(document).ready(function () {
     $.getJSON('convertcsv.json', function(data) {
     jsonUsers = data;
  });
});

Now when click on button, filter out results from jsonUsers

Upvotes: 3

Related Questions