I couldn't find anywhere on the web JSON file for download that has U.S state two-letter abbreviations as keys, and simple array of city names as values. I needed this JSON file to stick into AngularJS application, removing the need to pull data from the server.
What I did is - I've primarily found list of state abbreviations, and secondary U.S. Small business administrations API for listing all counties and cities within one state - which resulted in Groovy script generating wanted JSON file.
State list is taken from http://adamkinney.com/blog/2012/04/25/list-of-us-states-in-javascript-object-notation/, with some states removed (ones that SBA API did not recognize, like Palau)
Groovy script follows:
// list taken from http://adamkinney.com/blog/2012/04/25/list-of-us-states-in-javascript-object-notation/, and // transformed into Groovy notation def states = [ [ name: 'ALABAMA', abbreviation: 'AL'], [ name: 'ALASKA', abbreviation: 'AK'], [ name: 'ARIZONA', abbreviation: 'AZ'], [ name: 'ARKANSAS', abbreviation: 'AR'], [ name: 'CALIFORNIA', abbreviation: 'CA'], [ name: 'COLORADO', abbreviation: 'CO'], [ name: 'CONNECTICUT', abbreviation: 'CT'], [ name: 'DELAWARE', abbreviation: 'DE'], [ name: 'DISTRICT OF COLUMBIA', abbreviation: 'DC'], [ name: 'FLORIDA', abbreviation: 'FL'], [ name: 'GEORGIA', abbreviation: 'GA'], [ name: 'GUAM', abbreviation: 'GU'], [ name: 'HAWAII', abbreviation: 'HI'], [ name: 'IDAHO', abbreviation: 'ID'], [ name: 'ILLINOIS', abbreviation: 'IL'], [ name: 'INDIANA', abbreviation: 'IN'], [ name: 'IOWA', abbreviation: 'IA'], [ name: 'KANSAS', abbreviation: 'KS'], [ name: 'KENTUCKY', abbreviation: 'KY'], [ name: 'LOUISIANA', abbreviation: 'LA'], [ name: 'MAINE', abbreviation: 'ME'], [ name: 'MARYLAND', abbreviation: 'MD'], [ name: 'MASSACHUSETTS', abbreviation: 'MA'], [ name: 'MICHIGAN', abbreviation: 'MI'], [ name: 'MINNESOTA', abbreviation: 'MN'], [ name: 'MISSISSIPPI', abbreviation: 'MS'], [ name: 'MISSOURI', abbreviation: 'MO'], [ name: 'MONTANA', abbreviation: 'MT'], [ name: 'NEBRASKA', abbreviation: 'NE'], [ name: 'NEVADA', abbreviation: 'NV'], [ name: 'NEW HAMPSHIRE', abbreviation: 'NH'], [ name: 'NEW JERSEY', abbreviation: 'NJ'], [ name: 'NEW MEXICO', abbreviation: 'NM'], [ name: 'NEW YORK', abbreviation: 'NY'], [ name: 'NORTH CAROLINA', abbreviation: 'NC'], [ name: 'NORTH DAKOTA', abbreviation: 'ND'], [ name: 'OHIO', abbreviation: 'OH'], [ name: 'OKLAHOMA', abbreviation: 'OK'], [ name: 'OREGON', abbreviation: 'OR'], [ name: 'PENNSYLVANIA', abbreviation: 'PA'], [ name: 'PUERTO RICO', abbreviation: 'PR'], [ name: 'RHODE ISLAND', abbreviation: 'RI'], [ name: 'SOUTH CAROLINA', abbreviation: 'SC'], [ name: 'SOUTH DAKOTA', abbreviation: 'SD'], [ name: 'TENNESSEE', abbreviation: 'TN'], [ name: 'TEXAS', abbreviation: 'TX'], [ name: 'UTAH', abbreviation: 'UT'], [ name: 'VERMONT', abbreviation: 'VT'], [ name: 'VIRGIN ISLANDS', abbreviation: 'VI'], [ name: 'VIRGINIA', abbreviation: 'VA'], [ name: 'WASHINGTON', abbreviation: 'WA'], [ name: 'WEST VIRGINIA', abbreviation: 'WV'], [ name: 'WISCONSIN', abbreviation: 'WI'], [ name: 'WYOMING', abbreviation: 'WY' ] ], //this our map that will be seriazlied to JSON allData = [:] //iterate through all of the states states.each { it -> def twoLetterCode = it.abbreviation, //grab the data from the api citiesJson = new URL("http://api.sba.gov/geodata/primary_city_links_for_state_of/${twoLetterCode}.json").text, citiesMap = new groovy.json.JsonSlurper().parseText(citiesJson) allData[twoLetterCode] = [] //iterate through all cities and add them to result set citiesMap.each { city -> allData[twoLetterCode].push(city.name) } //sort alphabetically asceding (alphabetical sort is default for String object in Groovy) allData[twoLetterCode].sort() } //serialize Groovy map to JSON def serializedData = new groovy.json.JsonBuilder(allData).toPrettyString() //and write to file new File('/tmp/usCitiesData.json') << serializedData return true
You can see generated file here - http://pastebin.com/GSpzczfi
No comments:
Post a Comment