albuvee albuvee - 1 month ago 5
JSON Question

Filter JSON with jQuery

How can I filter a Google Spreadsheet JSON (like this one: https://spreadsheets.google.com/feeds/list/18wLFkAE92tx8JL2WeKFNv2dosT0qSHoHYqdhqS7xbQQ/1/public/values?alt=json), so all I get to work with is the json-entry, where there is a

"title":{"type":"text","$t":"test1"}
aka «where title = test1».

All I want to get out the full JSON is the one entry, where there is a child with title = test1 … from
id
to
gsx$articletitle
or what ever is in the same entry. To display only this one entry (filtered via title) on a website.

"id": {
"$t": "FULLURL"
},
… blablabla …
"title": {
"type": "text",
"$t": "test1"
},
… blablabla …
"gsx$articletitle": {
"$t": "Viva la Grischa"
}

Answer

Once the JSON is parsed into a variable:

var arrayOfObjects = JSON.parse(THE_STRING_FROM_GOOGLE).feed.entry;

you can filter it like so:

arrayOfObjects = arrayOfObjects.filter(function(o){
  return o.title.$t == "test1";
});

You can then get the values like so (a few examples):

var myFoundObject = arrayOfObjects[0];

console.log(myFoundObject.content.$t);
console.log(myFoundObject.content.type);
console.log(myFoundObject.category[0].scheme);
console.log(myFoundObject.category[0].term);
console.log(myFoundObject.title.$t);
console.log(myFoundObject.id.$t);

Working example:

https://jsfiddle.net/hkkkLz4y/1/

Comments