Retrieving data from Google Places API in JSON format

Having some trouble with the Places API, I initially attempted to use $.ajax from jQuery but kept encountering an unexpected token error on the first element of the file. It turns out that JSONP cannot be fetched from the Places API. Below is a snippet of the response it provides. Struggling to log the 'id' into the console...any guidance would be appreciated!

JSON returned by Places:

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 33.7862590,
               "lng" : -117.8531270
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "5be9a13a28635cf8fd2ef3c62c24d51277a4ba58",
         "name" : "Kimmies Coffee Cup",
         "rating" : 3.30,
         "reference" : "CnRpAAAANKLoOHJ1STFaFavIPjm2bpMDneGTv0AXfixyl-koF7KSK-lQSldk3c01RFJu9Gf62zXnRScte-dkPCBU8KuxCo7AMctfuaIhNkPE8RKENF9HeMlkuEg-Scp97--1VWJvjpNQjc6xRvdXsbf7V8NrSRIQnXjZBKbrSeSg2sTwBosKghoUEFlNDIN5cnVbn5n7aNtwycord-E",
         "types" : [ "restaurant", "food", "establishment" ],
         "vicinity" : "190 S Glassell St # B, Orange"
      }
      ]
}

And here is the code I was attempting to use (can't use it anymore):

 $(document).ready(function () {
    $.ajax({
      url: 'https://maps.googleapis.com/maps/api/place/search/json',
      dataType: 'jsonp',
      type: 'GET',
      data: {
        location: '33.787794,-117.853111',
        radius: 100,
        name: 'coffee',
        key: 'KEY_HERE',
        sensor: false,
      },
      success: function(data) {
        console.log(data.results[0].id);
    }
  });
});

Any tips or suggestions are welcome. Thank you!

Answer №1

There is a handy method in jQuery called parseJSON that can easily convert JSON data into a JavaScript object.

After converting the JSON data into an object, you can access the results array using obj.results.

To retrieve the id of the first result, simply use results[0].id.

Answer №2

What happens when the datatype json is utilized instead of jsonp?

Answer №3

Printing the id does not require a for loop, simply remove it and give it a try. Additionally, I'm unsure of what 'myResult' is as it seems to be undefined in this context.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I ensure that the results retrieved from the Pocket API JSON are always in a single row? Let's find a solution

I have been attempting for hours and conducting a thorough search to extract a dataframe from the list retrieved from my Pocket API. However, my code is aggregating everything into a single row, which is not the desired outcome. I have made numerous atte ...

Pace.js doesn't bother waiting for iframes to completely load before moving on

On my website, I am using pace.js but encountering issues with pages containing iframes. Is there a method to ensure that pace.js considers the content loading within those iframes? I attempted to configure paceOptions to wait for the iframe selector to l ...

Is your Ajax jQuery live search not functioning properly with JSON data?

My programming code is not functioning properly. Here is the file I am working on. When it does work, it does not display the list and gives an error in the Json file. I am unsure of the reason behind this issue. You will be able to view the error in the C ...

My jquery filter is not functioning properly

I need help making the active class work on each category when clicked, shifting to the next one. I've tried implementing it but no luck so far. $(document).ready(function() { $('.list-inline-item').click(function() { const value = ...

An error occurred while attempting to save a new entry using the New Entry Form in DataTable, stating that the variable "

I have encountered an issue with a table that includes a bootstrap modal containing a form. After filling out the form and saving the data or closing the modal, I want to refresh the table data. However, I am receiving the following error: TypeError: c i ...

There seems to be an issue with the AJAX REST call failing to transmit data

Whenever I attempt to submit the form, the page refreshes and nothing gets saved in the database. The code in subforum.js $(document).on('click','#submit',function(e) { var user = JSON.parse(sessionStorage.getItem("ulogovan")); consol ...

How come the date displays as 21/1/2015 instead of 21/1/2015 in Android after parsing the JSON data?

Currently, I am utilizing the DatePicker functionality in my code and transmitting the value via JSON. The desired format for the value is 21/1/2015 without the extra backslashes. How can I resolve this issue? DatePickerDialog.OnDateSetListener date = n ...

Mastering the Art of Accelerating getJSON Array Data

Currently, I am facing a challenge with retrieving a large array (4MB) of data from the server side. I have been utilizing the jQuery getJSON method to obtain the array data and display it on the browser. However, this process has proven to be quite slow ...

Navigating through Leaflet to reference a .json file

Looking to integrate a .json vector layer into a Leaflet.js map, which can be seen on the GitHub page here, with the source code available here. Here's a condensed version of the file for reference (full version visible on the linked GitHub page). & ...

Choose a SQL Statement Based on Conditions

My task involves sending Thank You Messages by posting data to a specific URL. The information I need is retrieved from a table named readtextfilejson, where details of users are stored for sending the messages. The challenge I am facing is in selecting o ...

Utilize Jquery to determine the upload speed bandwidth

Is there a method for measuring upload speed using jquery or javascript? We currently calculate download speed by utilizing pseudostreaming to determine the total time it takes to receive the response. Can we apply a similar technique to measure upload sp ...

"Receiving HTML response from an HTTP GET request in Angular 2

Attempting to transmit test data from my node.js server to the front end. router.get('/abc', (req, res) => { return res.json({ test: "success!" }); }); In my service component: private url = "http://localhost:4200/auth/abc"; getTitl ...

Issue encountered with IONIC data tables in Ionic 1.x when integrating with Angular 1.x

I am attempting to integrate angular datatables from this source, but encountering the following error ionic datatable error snippet <head> <script src="js/controllers.js"></script> <script src="js/services.js"></script> ...

Grouping JSON data by a specific key is the key to better organization

Assume the JSON data looks like this: $string = '{ "John": { "status":"Wait", "id":"001" }, "Jennifer": { "status":"Active", "id":"002" }, "James": { "status":"Active", "age":56, "count":10, "progress":0.0029857, ...

How to utilize "json_encode" function for accessing array elements

I'm currently working on fetching a JSON request from the query string ID, and it seems to be functioning almost correctly. However, an additional array object is being added. $id = $_GET['id']; $result = mysqli_query($con,'SELECT * F ...

Verifying Username with AJAX and JSON Technology

In a PHP file, I have stored usernames in JSON format. [ {"korisnicko_ime":"darbranis"}, {"korisnicko_ime":"markrc"}, {"korisnicko_ime":"leoluk"}, {"korisnicko_ime":"borbau"}, {"korisnicko_ime":"ivavad"}, {"korisnicko_ime":"andtikv ...

detecting key presses on documents using javascript

I'm having trouble capturing document-level key press events on a webpage. I've tried using the following code: $(document).bind('keydown', 'a', keyevent_cb); It works fine in IE, but it's not consistent in Firefox. I&a ...

To effectively execute a JQuery / Javascript function, it is essential to incorporate both $(document).ready and $(document).ajaxSucces

I have a JavaScript function that is used for basic UI functionality across my site. Some elements affected by this function are injected via Ajax, while others are static HTML. Currently, I have duplicated the function and applied it to both $(document). ...

Transferring data through Ajax, Jquery, and PHP: A seamless process

My goal is to develop an attendance button that adds +1 to the total number of people attending when clicked by the user. I am looking for a way to achieve this without having to refresh the page. Ideally, I would like to connect it to a database to upda ...

Is it possible for CSS to prevent the insertion of spaces?

When filling out a form, I am able to insert spaces in inputs but not in the textarea (which is necessary). Interestingly, inserting spaces in the textarea works flawlessly. <form action="/#wpcf7-f519-o1" method="post" class="wpcf7-form" enctype="mu ...