Mithrilhall
Mithrilhall

Reputation: 1502

How to search json structure

I'm attempting to search through this json for a specific value.

I would like to search by the State value, such as 'NH' and get back the MaintenancePercentage and OfficePercentage.

I've tried looping through payrollDefaults.State and I'm getting back nothing.

var payrollDefaults =
    [
        {
            "State": "NH",
            "MaintenancePercentage": 21.45,
            "OfficePercentage": 22.56
        },
        {
            "State": "NC",
            "MaintenancePercentage": 21.79,
            "OfficePercentage": 22.41
        },
        {
            "State": "OH",
            "MaintenancePercentage": 21.33,
            "OfficePercentage": 22.25
        },
        {
            "State": "RI",
            "MaintenancePercentage": 23.04,
            "OfficePercentage": 23.22
        },
        {
            "State": "SC",
            "MaintenancePercentage": 21.31,
            "OfficePercentage": 31.33
        },
        {
            "State": "TX",
            "MaintenancePercentage": 35.6,
            "OfficePercentage": 24.48
        },
        {
            "State": "VA",
            "MaintenancePercentage": 32.32,
            "OfficePercentage": 30.47
        },
        {
            "State": "AL",
            "MaintenancePercentage": 13.33,
            "OfficePercentage": 12.42
        },
        {
            "State": "CA",
            "MaintenancePercentage": 49.37,
            "OfficePercentage": 59.67
        },
        {
            "State": "FL",
            "MaintenancePercentage": 34.62,
            "OfficePercentage": 31.06
        },
        {
            "State": "GA",
            "MaintenancePercentage": 35.55,
            "OfficePercentage": 30.29
        },
        {
            "State": "IN",
            "MaintenancePercentage": 33.45,
            "OfficePercentage": 30.69
        },
        {
            "State": "MA",
            "MaintenancePercentage": 32.9,
            "OfficePercentage": 29.34
        },
        {
            "State": "MI",
            "MaintenancePercentage": 34.95,
            "OfficePercentage": 29.46
        },
        {
            "State": "MT",
            "MaintenancePercentage": 35.91,
            "OfficePercentage": 30.02
        }
    ]

EDIT:

I ended up converting this from a string to JSON.

var json = $.parseJSON(payrollDefaults);

Upvotes: 0

Views: 162

Answers (8)

Hakan Bilgin
Hakan Bilgin

Reputation: 1122

You could use this JS lib, DefiantJS (http://defiantjs.com) - which extends the global object JSON with the method "search". With this method, you can search a JSON structure with XPath expressions and it returns an array (empty if no matches were found).

var payrollDefaults = [
        { "State": "NH", "MaintenancePercentage": 21.45, "OfficePercentage": 22.56 },
        { "State": "NC", "MaintenancePercentage": 21.79, "OfficePercentage": 22.41 },
        { "State": "OH", "MaintenancePercentage": 21.33, "OfficePercentage": 22.25 },
        { "State": "RI", "MaintenancePercentage": 23.04, "OfficePercentage": 23.22 },
        { "State": "SC", "MaintenancePercentage": 21.31, "OfficePercentage": 31.33 },
        { "State": "TX", "MaintenancePercentage": 35.6,  "OfficePercentage": 24.48 },
        { "State": "VA", "MaintenancePercentage": 32.32, "OfficePercentage": 30.47 },
        { "State": "AL", "MaintenancePercentage": 13.33, "OfficePercentage": 12.42 },
        { "State": "CA", "MaintenancePercentage": 49.37, "OfficePercentage": 59.67 },
        { "State": "FL", "MaintenancePercentage": 34.62, "OfficePercentage": 31.06 },
        { "State": "GA", "MaintenancePercentage": 35.55, "OfficePercentage": 30.29 },
        { "State": "IN", "MaintenancePercentage": 33.45, "OfficePercentage": 30.69 },
        { "State": "MA", "MaintenancePercentage": 32.9,  "OfficePercentage": 29.34 },
        { "State": "MI", "MaintenancePercentage": 34.95, "OfficePercentage": 29.46 },
        { "State": "MT", "MaintenancePercentage": 35.91, "OfficePercentage": 30.02 }
    ],
    res = JSON.search( payrollDefaults, '//*[State = "NH"]' );

console.log( res[0].MaintenancePercentage );
console.log( res[0].OfficePercentage );

Here is a working fiddle:
http://jsfiddle.net/hbi99/dMLdc/

Upvotes: 1

Tom Bowers
Tom Bowers

Reputation: 5140

This should do the trick...

http://jsfiddle.net/6zn7H/

function findState(state) {
    for(var i =0, l = payrollDefaults.length; i < l; i++){
        var value = payrollDefaults[i];
        if(value.State === state){
            return value;
        }
    }
}

Also note that this loop only evaluates the length once, and not on each iteration, helping efficiency.

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 9612

Try this out:- http://jsfiddle.net/ba5ZQ/1/

JS:-

var arr = jQuery.grep(payrollDefaults, function( a ) {
  return a.State =="NH";
});

console.log(arr);

Upvotes: 0

iJade
iJade

Reputation: 23801

Here is how you need to search for a particular state

function GetMaintainancePercentage(state) {
    for (var i = 0; i < payrollDefaults.length; i++) {
        if (payrollDefaults[i]["State"] == state) {
            alert('Maintainance Percenatge is ' + payrollDefaults[i]["MaintenancePercentage"]);
        }
    }
}

Here is a demo

Upvotes: 0

ovunccetin
ovunccetin

Reputation: 8663

Try the below function:

function findByState(givenState) {
    var result = [];
    for(p in payrollDefaults) {
        if (p.State === givenState) {
            result.push(p);
        }
    }

    return result;
}

Upvotes: 0

forivall
forivall

Reputation: 9901

Using lodash.js's find

_.find(payrollDefaults, {State: 'NH'});
>>> {
        "State": "NH",
        "MaintenancePercentage": 21.45,
        "OfficePercentage": 22.56
    }

Upvotes: 0

Robin
Robin

Reputation: 864

$.each(payrollDefaults,function(key,val){
  if(val.state == "NH"){
   console.log(val.MaintenancePercentage);
   console.log(val.OfficePercentage);
  }

});

This should help you

Upvotes: 0

guypursey
guypursey

Reputation: 3184

You need to provide an index for the payrollDefaults array else your program won't know which specific object you want to pull the value of State from.

Just as an example, here's how to loop through all the State properties and print their values in the console.

for (var i = 0, l = payrollDefaults.length; i < l; i += 1) {
    console.log(payrollDefaults[i].State);
}

Notice the [i] and how that refers to a specific object in your array?

Hope that helps make sense of what you were missing!

Upvotes: 0

Related Questions