4

I have a huge JSON code with over 4k lines to push info from and i don't really know whats the best way to push/return the string values that i need.

Note: the code is not actually a json file it's inside a string

I was thinking in maybe using slice for some of the info that i want and pushing into a array using the indexOf(), but this is probably the worst way.

eg.

product {
  'name' : 'Backpack',
  'color' : 'Blue',
  'id' : 101,
  'is_in_stock' : 'true'
  'image_url' : {
    'link_1' : 'url',
    'link_2' : 'url2'
  }
}

product {
  'name' : 'Backpack',
  'color' : 'Red',
  'id' : 102,
  'is_in_stock' : 'true'
  'image_url' : {
    'link_1' : 'url',
    'link_2' : 'url2'
  }
}

product {
  'name' : 'Backpack',
  'color' : 'Green',
  'id' : 103,
  'is_in_stock' : 'true'
  'image_url' : {
    'link_1' : 'url',
    'link_2' : 'url2'
  }
}

Basically need to build a function to filter id and other stuff?

Leo
  • 158
  • 6

1 Answers1

1

I generated a sample Json file at "https://www.json-generator.com/api/json/get/cqTefoahua?indent=2" which contains more than 7000 lines (>500 Objects).

var json = $.getJSON("https://www.json-generator.com/api/json/get/cqTefoahua?indent=2");

function searchArray(property,value){
  var t0 = performance.now();
  var result = json.responseJSON.find(someobject => someobject[property] === value);
  var t1 = performance.now();
  console.log("Call to find took " + (t1 - t0) + " milliseconds.");
  return result;
}
//After Json loaded 
 var item1 = searchArray("index","100") // Call to find took 0.07499987259507179 milliseconds.    
 var item2 = searchArray("index","400") // Call to find took 0.090000219643116 milliseconds.

You can edit the returned value.

Murali Nepalli
  • 1,588
  • 8
  • 17
  • Hmm i keep getting result undefined not sure where im getting the function wrong. I think this doesn't really help me since my line of code isn't really a JSON, its as a string from JSON. It would be the same as your JSON.stringify(json) – Leo May 03 '19 at 14:15
  • @Leo : you should call method searchArray after json is loaded. Call searchArray in in getJSON callback. If it's a string from JSON cant you convert that back to JSON obj and parse back to array? – Murali Nepalli May 03 '19 at 14:31
  • Please check this thread https://stackoverflow.com/questions/4935632/parse-json-in-javascript – Murali Nepalli May 03 '19 at 14:37