0

I have a JSON file with city names and their ids. I want to load only the city names into a script variable so I can use that variable as a source in autocomplete for jQuery.

Data Example:

{
    "cities": [
        {
            "id": 1440,
            "name": "Bundi"
        },
        {
            "id": 850,
            "name": "Beed"
        },
        {
            "id": 1409,
            "name": "Kankroli"
        },
        {
            "id": 1913,
            "name": "Muniyal Herbals And Ayurvedic Remedies Limited"
        },
        {
            "id": 2418,
            "name": "Kunnicode"
        }]
}
2
  • names = cities.map(function(v){ return v.name }) Commented Jun 19, 2013 at 5:24
  • Thank u that line helped at the end lot... Commented Jun 19, 2013 at 11:50

1 Answer 1

1
var someVar = JSON.parse(someStringContainingYourJSON) 


someVar.cities[0].id.. etc
someVar.cities[1].id

or you can use

$.getJSON('/url',function(someVar){
   someVar.cities[0].id.. etc
    someVar.cities[1].id

})
Sign up to request clarification or add additional context in comments.

7 Comments

i have almost 20k line of names.so i want them to be in json file and load them from file.
then you should use jquery ajax to load it.
if you have a string with the json, use JSON.parse
can u please give me a sample how to do that.Suppose if file name is cities.json.wat would be the code in jquery ajax.
try the above using getJSON
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.