14

How to fine state name using postcode in bellow json data;

var data = '{
  "1": {
    "state": "VIC",
    "postcode": "2600,2603,2605,2606"
  },
  "2": {
    "state": "NSW",
    "postcode": "2259,2264"
  }
}'

How to find state by postcode;

if i search postcode 2600 if get result like VIC

Jayesh Vekariya
  • 163
  • 1
  • 1
  • 6
  • first, you want to JSON.parse(data) (though, that wont work as is, because what you've posted is not valid javascript) - then it's just a regular object you'll be dealing with – Jaromanda X Dec 24 '15 at 10:03
  • 1
    `if i search postcode` - how are you "searching" ... show some code – Jaromanda X Dec 24 '15 at 10:03
  • passing postcode value in some function and function return state name – Jayesh Vekariya Dec 24 '15 at 10:04
  • 1
    no matter what you do with this data, it will be wrong ... postcodes in the range 2600->2639 are not **VIC** – Jaromanda X Dec 24 '15 at 10:15
  • I assume from the Javascript tag that you want to parse it in Javascript, but if you just need to extract values from a file containing JSON data, your best bet is `jq`: https://stedolan.github.io/jq/ – Gaurav Dec 24 '15 at 11:06
  • https://gist.github.com/iwek/3924925 worked for me – frumbert Jun 23 '17 at 10:42

5 Answers5

13

Remove '' as yours is not a valid string, remove '' to make it a valid object literal, then you can iterate over the keys of the Object and check if it has the matching POSTCODE and if it has then return it's corresponding state.

var data = {
  "1": {
    "state": "VIC",
    "postcode": "2600,2603,2605,2606"
  },
  "2": {
    "state": "NSW",
    "postcode": "2259,2264"
  }
};

function getState(data, postcode){

  for(var x in data){
    if(data[x].postcode && data[x].postcode.split(",").indexOf(postcode.toString())!=-1) return data[x].state;
  }
  
  return "Not Found";
  
}

alert(getState(data, "2600"));
alert(getState(data, 2264));

You can directly do .indexOf on the postcode, even without using .split(","). But, then, it will also match with ,2600 which should not be the case. So, use split.

Use json[x].postcode condition to make sure that postcode field exists in the object. Otherwise, it will give an error if it does not exist.

void
  • 36,090
  • 8
  • 62
  • 107
7

Try like this

var data = '{"1": { "state": "VIC","postcode": "2600,2603,2605,2606"}, "2": {"state": "NSW","postcode": "2259,2264"}}';
var jsObj = JSON.parse(data);
var find = "2600";

var values = Object.keys(jsObj).filter(function(x) {
  return jsObj[x].postcode.indexOf(find) > -1;
}).map(function(x) {
  return jsObj[x].state;
});

console.log(values.length > 0 ? values[0] : "not found");

JSFIDDLE

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
  • neither is yours, @CodeiSir - consider real world data of 10's of thousands of postcodes ... none of the answers posted will perform very well – Jaromanda X Dec 24 '15 at 10:33
  • @JaromandaX how would you make "mine" faster? (wich is pretty much the same as the accepted answer, appart from the `.toString()` that is called way to often :P – CoderPi Dec 24 '15 at 10:35
  • 1
    I couldn't make yours or anyone else's faster - I'd write it in a different way - you brought up performance, I was simply pointing out performance for this simple example is negligible amongst all answers – Jaromanda X Dec 24 '15 at 10:37
5

function findState(data, postcode) {
  var postcode = postcode.toString()
  for (var k in data) {
    var postcodes = data[k].postcode.split(",")
    if (postcodes.indexOf(postcode) != -1)
      return data[k].state
  }
}

// Demo Output
var data = '{"1":{"state":"VIC","postcode":"2600,2603,2605,2606"},"2":{"state":"NSW","postcode":"2259,2264"}}'
var dataObj = JSON.parse(data)

var state = findState(dataObj, 2600)
document.write(state)
CoderPi
  • 12,985
  • 4
  • 34
  • 62
2

You can try something like this:

function searchInObject(object, searchKey, searchValue) {
  for (var i in object) {
    if (object[i][searchKey].indexOf(searchValue) > -1) {
      return object[i];
    }
  }
}

(function() {
  var data = {
    "1": {
      "state": "VIC",
      "postcode": "2600,2603,2605,2606"
    },
    "2": {
      "state": "NSW",
      "postcode": "2259,2264"
    }
  }

  var pin = "2600";
  var result = searchInObject(data, "postcode", pin);
  console.log(result.state);

  pin = "2259";
  result = searchInObject(data, "postcode", pin);
  console.log(result.state);
})()
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1

Well now.. You are just asking us to help you with homework :) Good thing im in a good mood.

First get a proper JSON string, and parse it into a object using JSON.parse. Then iterate this object and split the postcode string and find the state!

var data = ......
var resp = JSON.parse(data);

function getStateByPostcode(postcode) {
  var state = "";
  for(var i in resp) {
    if(resp.hasOwnProperty(i)) {
       var postcodes = resp[i]['postcode'].split(',');
       if(postcodes.indexOf(postcode) !== -1) {
           return resp[i]['state'];
       }
    }
  }
  return state;
}
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149