1

I have the following JSON (truncated):

{
    "** Please contact a mod by posting on the forums **": {
        "tvdb_id": "257937",
        "last_updated": 1341780286,
        "images": {
            "poster": "http://zapp.trakt.us/images/posters/17288.jpg",
            "fanart": "http://zapp.trakt.us/images/fanart/17288.jpg"
        }
    },
    "Jamies Best Ever Christmas": {
        "tvdb_id": "214161",
        "last_updated": 1329701153,
        "images": {
            "poster": "http://zapp.trakt.us/images/posters/9126.jpg",
            "fanart": "http://zapp.trakt.us/images/fanart/9126.jpg"
        }
    },
    "Kuromajo-san ga Tooru!!": {
        "tvdb_id": "257914",
        "last_updated": 1395775431,
        "images": {
            "poster": "http://zapp.trakt.us/images/posters/15640.jpg",
            "fanart": "http://zapp.trakt.us/images/fanart/15640.jpg"
        }
    },
    "Arbesre Shavuout": {
        "tvdb_id": "253960",
        "last_updated": 1328789472,
        "images": {
            "poster": "http://zapp.trakt.us/images/posters/14058.jpg",
            "fanart": "http://zapp.trakt.us/images/fanart/14058.jpg"
        }
    }
}

I want to search a value (from a var for now) and find the key to return the tvdb_id.

For example if I search for “Jamies Best Ever Christmas” I want to return the respective tvdb_id.

Would something like this help?

function findKey(obj, value) {
  var key;

  _.each(obj, function (v, k) {
    if (v === value) {
      key = k;
    }
  });

  return key;
}

How do I implement it? (JSON newbie)

Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
Alex
  • 1,576
  • 3
  • 17
  • 35

3 Answers3

5

In your example the value you have is the key, so just use it to access the property you want..

var somevalue = 'Jamies Best Ever Christmas',
    tvdb_id = jsonobj[somevalue].tvdb_id;
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Thanks, not exactly what I’m looking for but helpful. Rephrased my question: http://stackoverflow.com/questions/22797839/json-file-read-with-select2-plugin – Alex Apr 01 '14 at 22:07
2

In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method:

var keys = Object.keys(myJsonObject);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

You may use the one below as well:

var getKeys = function(obj){
   var keys = [];
   for(var key in obj){
      keys.push(key);
   }
   return keys;
}
pj013
  • 1,393
  • 1
  • 10
  • 28
0

It looks like you're using Underscore.js.

Gaby's answer should fit the bill. However, if you're looking for partial matches, Underscore.js has some cool features in it. Consider this:

var mappedResults = _.map(jsonobj, function(key, value, list) {
    return {name:key, tvdb_id:value.tvdb_id};
});

This will give you an array of objects, each with the title and the tvdb id.

To execute searches, you could write a function like this:

function getMatchingIds(searchterm) {
    return _.filter(mappedResults, function(entry) {
        return (entry.name.indexOf(searchterm) != -1);
    });
}
Vishal Kotcherlakota
  • 1,134
  • 4
  • 13
  • 36