title: Can't parse JSON formatted response from server
body: <p>I'm trying to parse a JSON formatted string like</p>
  
  <pre><code>var json_response = JSON.parse(response); 
  </code></pre>
  
  <p>response is as below. It's a formatted JSON response from a Google api.</p>
  
  <pre><code>{
   "location": {
    "lat": 12.9621211,
    "lng": 77.64804099999999
   },
   "accuracy": 740.0
  }
  </code></pre>
  
  <p>However I get an error saying</p>
  
  <pre><code>Uncaught SyntaxError: Unexpected token {
  </code></pre>
  
  <p>I checked out a lot of answers in SO. A lot of them say the above json response is already an object. But when i tried</p>
  
  <pre><code>console.log(response["location"]);
  console.log(response.location);
  </code></pre>
  
  <p>I get the following output</p>
  
  <pre><code>undefined
  undefined
  </code></pre>
  
  <p>What am I doing wrong ? </p>
  
  <p>EDIT:</p>
  
  <pre><code>console.log(response);
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>{
   "location": {
    "lat": 12.9621211,
    "lng": 77.64804099999999
   },
   "accuracy": 740.0
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>When i tried the following</p>
  
  <pre><code>console.log('"'+response+'"');
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>"{
   "location": {
    "lat": 12.962118199999999,
    "lng": 77.6480399
   },
   "accuracy": 739.0
  }
  
  "
  </code></pre>
  
  <p>Seems like there is an extra line after the closing }. Will that make any difference ?</p>
  
  <p>I'm pasting my whole function here. sorry if i was not clear before.</p>
  
  <pre><code>    function get_distance_from_cellTower(json){
          $.ajax({type: 'POST', url:"get_location.php",data:getformurlencoded(json),
              success:function(response){
                  console.log('"'+response+'"');
                  var latitude;
                  var longitude;
                  var success;
                  var json_response;
                  try{
                      json_response = JSON.parse(response);
                      if(json_response.hasOwnProperty("error")){
                          success = 0;
                          console.log(json_response.error);
                          append_to_show(json_response.error);
                      }
                      if(json_response.hasOwnProperty("location")){
                          success = 1;
                      }
                  }
                  catch(e){
                      console.log(e);
                      append_to_show(e);
                  }
  
                  if(success){
                      var location = JSON.parse(json_response.location);
                      latitude = parseFloat(location.lat);
                      longitude = parseFloat(location.lng);
                      var distance = calculate_distance_kms(latitude, doclat, longitude, doclong);
                      append_to_show("cell tower: "+distance);
                      console.log("Cell tower: "+distance);
                  }
              },
  
              error:function(err){
                  console.log(err);
                  append_to_show(err);
              },contentType:'application/x-www-form-urlencoded'});
  
      }  
  </code></pre>