title: How do I stringify a google LatLng object?
body: <p>I'm working with the google maps API and trying to stringify an array of points, which each have a latitude and longitude. What I want to do is stringify the data and store it in a database so that it can be retrieved later. The problem is that, because of the way google maps stores and uses data, the result of <code>JSON.stringify</code> is strangely random at times. For example, sometimes the data looks like this:</p>
  
  <pre><code>[{"Na":35.99663,"Oa":-78.94982},
   {"Na":35.996370000000006,"Oa":-78.94914},
   {"Na":35.99595,"Oa":-78.94833000000001},...]
  </code></pre>
  
  <p>And sometimes the exact same data looks like this:</p>
  
  <pre><code>[{"Ia":35.99663,"Ja":-78.94982},
   {"Ia":35.996370000000006,"Ja":-78.94914},
   {"Ia":35.99595,"Ja":-78.94833000000001},...]
  </code></pre>
  
  <p>In fact I've seen about ten different variations. This causes a lot of problems when I store the stringified data in the database and retrieve it, because I never know which format to expect. What I want to do is replace the keys so that the data looks more like this:</p>
  
  <pre><code>[{"lat":35.99663,"lon":-78.94982},
   {"lat":35.996370000000006,"lon":-78.94914},
   {"lat":35.99595,"lon":-78.94833000000001},...]
  </code></pre>
  
  <p>I've already written code to figure out which random letters google maps is using. With that information, I want to replace each occurrence of those random letters with "lat" or "lon". I hope to use a replacer method like so...</p>
  
  <pre><code>function formatData(data) {
      //data is an object that I want to stringify
  
      //this testPoint and testString help us figure out which letters google is using this time around
      var testPoint = new google.maps.LatLng(23,45);
      var testString = JSON.stringify(testPoint);
      var strangeLat = testString.substring(2,4); //this is a random two character string which represents latitude in the stringified data
      var strangeLon = testString.substring(10,12); //this is a random two character string which represents longitude in the stringified data
  
      //stringify the data and replace all occurences of the random letters with lat and lon
      var formattedData = JSON.stringify(data, function(key,value) {
          //do something here to replace the keys
      });
  
      return formattedData;     
  }
  </code></pre>
  
  <p>I know how to replace values with the replacer function, but I can't figure out how to replace keys. I searched around and found a few similar cases online: <a href="https://stackoverflow.com/questions/5480570/json-stringify-change-the-case-of-the-key">JSON.stringify, change the case of the key</a> and <a href="https://stackoverflow.com/questions/4433402/replace-keys-json-in-javascript">Replace keys JSON in javascript</a> . However, I had trouble understanding them and was unable to come up with a solution to fit my needs. </p>
  
  <p>Thanks in advance for any help or suggestions!</p>
  
  <hr>
  
  <h2>Update!</h2>
  
  <p>Thanks all. I was able to come up with a solution using a combination of your suggestions. Here's what it looks like:</p>
  
  <pre><code>function formatData(data) {
      //data is an array of many LatLng objects
  
      newData = [];
  
      for(var i = 0; i &lt; data.length; i++) {
          var obj = new Object();
          obj['lat'] = data[i].lat();
          obj['lon'] = data[i].lng();
          newData.push(obj);
      }
  
      return JSON.stringify(newData);     
  }
  </code></pre>