Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JSON Key from Value or Inverting JSON Data

Getting single key from Value

I would like to do a backwards selection from the following JSON. I'd like to extract the abbreviation for a particular state. In this situation, the abbreviation is the key, and the value that I'm starting with is the value.

Certainly I can loop through each value, comparing the value to my value, and select the key when the match is made. Is this the best way to approach something like this? Or is there a better way?

Inverting JSON Data

Another option would to invert this data early in processing to give myself a similar set of values with the keys/values swapped. I would be interested in seeing methods for doing this efficiently as well.

var States = {AL: 'Alabama', AK: 'Alaska', AZ: 'Arizona', AR: 'Arkansas', 
              CA: 'California', CO: 'Colorado', CT: 'Connecticut', 
              DE: 'Delaware', DC: 'District of Columbia', FL: 'Florida', 
              GA: 'Georgia', HI: 'Hawaii', ID: 'Idaho', IL: 'Illinois', 
              IN: 'Indiana', IA: 'Iowa', KS: 'Kansas', KY: 'Kentucky', 
              LA: 'Louisiana', ME: 'Maine', MD: 'Maryland', MA: 'Massachusetts', 
              MI: 'Michigan', MN: 'Minnesota', MO: 'Missouri', MT: 'Montana', 
              NE: 'Nebraska', NV: 'Nevada', NH: 'New Hampshire', 
              NJ: 'New Jersey', NM: 'New Mexico', NY: 'New York', 
              NC: 'North Carolina', ND: 'North Dakota', OH: 'Ohio', 
              OK: 'Oklahoma', OR: 'Oregon', PA: 'Pennsylvania', 
              RI: 'Rhode Island', SC: 'South Carolina', 
              SD: 'South Dakota', TN: 'Tennessee', TX: 'Texas', UT: 'Utah', 
              VT: 'Vermont', VA: 'Virginia', WA: 'Washington', 
              WV: 'West Virginia', WI: 'Wisconsin', WY: 'Wyoming'};
like image 529
Sampson Avatar asked Dec 28 '09 16:12

Sampson


People also ask

How do I get key-value pairs in JSON?

In order to get a key-value pair from a KiiObject, call the get() method of the KiiObject class. Specify the key for the value to get as the argument of the get() method. The value of the key at the first level of the JSON document hierarchy will be obtained.

Can JSON have value without key?

JSON doesn't have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs.

What data type are the keys in JSON?

Keys and values are separated by a colon. Keys must be strings, and values must be a valid JSON data type: string.

Should JSON keys be strings?

In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type. An important difference, however, is that while Python dictionaries may use anything hashable as a key, in JSON all the keys must be strings.


2 Answers

There's no "automatic" way to do this. Your only option is to loop through the list until you find the value that matches the key.

But, if you need to do this multiple times, you should have the code rebuild the JSON object with key/values swapped, so that future lookups are faster. A simple way:

function swapJsonKeyValues(input) {
    var one, output = {};
    for (one in input) {
        if (input.hasOwnProperty(one)) {
            output[input[one]] = one;
        }
    }
    return output;
}

var stateAbbrs = swapJsonKeyValues(States);
like image 122
philfreo Avatar answered Oct 31 '22 14:10

philfreo


The only other logical solution would be to have the long name be the key and the abbreviation be the value. Selection is usually made on keys since they are unique, as they should.

You could save yourself from iterating every time you want to get the value, by doing the key - value switch the first time.

function switcharoo(o) {
    var t = {};
    for (var i in o) {
        if(o.hasOwnProperty(i)){
            t[o[i]] = i ;
        }
    }
    return t;
}

console.log(switcharoo({AZ: "Arizona"}));
like image 21
Luca Matteis Avatar answered Oct 31 '22 14:10

Luca Matteis