title: Use Google Maps API to get City from Zip code and then write it to JSON?
body: <p>I have a JSON file with a bunch of different zip codes. What I want to do is leverage the Google Maps API to retrieve the City associated with a zip code, and then write it to the JSON file.</p>
  
  <p>So far I have a script which is correctly grabbing the city from the results. How would I go about writing these cities to the JSON file?</p>
  
  <p><strong>Here's my JSON data:</strong></p>
  
  <pre><code>[
    {
      "State": "Oklahoma",
      "ZIP": 73169
    },
    {
      "State": "Oklahoma",
      "ZIP": 73169
    },
    {
      "State": "Oklahoma",
      "ZIP": 73170
    },
    {
      "State": "Oklahoma",
      "ZIP": 73172
    },
    {
      "State": "Oklahoma",
      "ZIP": 73173
    }
  ]
  </code></pre>
  
  <p><strong>And here's my script:</strong></p>
  
  <pre><code>$(function() {
    $.get('locations.json', function(data) {
      $.each(data, function(i, item) {
        var zip = item.ZIP;
        var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&amp;key=AIzaSyD_YqB4d_-xKcmNP9jJCiPkJYDS8J3f6pI";
        $.get(url, function(loc) {
            for(var i=0;i&lt;loc.results.length;i++) {
              var address = loc.results[i].formatted_address;
              var city = address.split(',', 1)[0]
              console.log(city);
            }
        });
      });
    });
  });
  </code></pre>
  
  <p><a href="https://plnkr.co/edit/0j6Q00XJbfWkGNslroD9?p=preview" rel="nofollow noreferrer">Here's a Plunker</a></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I found a way to write to my JSON file using NodeJS <a href="https://stackoverflow.com/questions/18508834/how-to-write-a-script-to-edit-a-json-file">from this question</a>, the code below successfully updates the "City" to "test" for each of my JSON objects, now I guess I just need help on incorporating this into my jQuery code:</p>
  
  <pre><code>fs = require('fs');
  var m = JSON.parse(fs.readFileSync('locations.json').toString());
  m.forEach(function(p){
      p.City = "test";
  });
  fs.writeFile('locations.json', JSON.stringify(m));
  </code></pre>
  
  <p>I would also be willing to use PHP if that would be easier.</p>
  
  <h1><strong>Yet another attempt(closest to what I'm trying to do):</strong></h1>
  
  <p>The plan is to only use this script once to populate the JSON with all of the cities. So I figured I would try to do it client-side where I can just log out some JSON data and then copy/paste it manually rather than relying on server-side code to do it for me. That brought me to this solution:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() {
     var item = [];
     var locations = [
       {
         "State": "Oklahoma",
         "ZIP": "73169",
         "City": ""
       },
       {
         "State": "Oklahoma",
         "ZIP": "73169",
         "City": ""
       },
       {
         "State": "Oklahoma",
         "ZIP": "73170",
         "City": ""
       },
       {
         "State": "Oklahoma",
         "ZIP": "73172",
         "City": ""
       },
       {
         "State": "Oklahoma",
         "ZIP": "73173",
         "City": ""
       }
     ];
  
        $.each(locations, function(i, item) {
      var zip = item.ZIP;
      var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&amp;key=AIzaSyA75wif8_yewifGSqVFnR3U50zuW_EnAns";
      $.get(url, function(loc) {
       for(var i=0;i&lt;loc.results.length;i++) {
        var address = loc.results[i].formatted_address;
        var city = address.split(',', 1)[0];
        item.City = city;
        console.log(item);
       }
      });
        });
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>If I log out <code>item</code> within the <code>for</code> statement, it returns everything correctly. How could I output this as a single array in JSON format?</p>