I need to take a JSON object and get the titles console logged for an auto complete feature. AN example of what my json looks like is below:
[ { "title": "Example 1", "url": "http:\/\/www.example1.com\/" }, { "title": "Example 2", "url": "http:\/\/www.example2.com\/" }, { "title": "Example 3", "url": "http:\/\/www.example3.com\/" }, ... I would like to log all the titles in my console like this:
Example 1 Example 2 Example 3 My initial attempt to do what I want was this:
$.ajax({ type: "GET", dataType: 'json', url: "/url/to/json/", success: function(data) { var searchResults = JSON.parse(data); console.log(searchResults.title); }, }); This returned:
Unexpected token o in JSON at position 1
After some further research:
Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()
It is suggested that the data is already parsed. So I try to call the object directly as these answers suggest:
$.ajax({ type: "GET", dataType: 'json', url: "/url/to/json/", success: function(data) { console.log(data.title); }, }); This gives me:
undefined
How do I print specific JSON data in console, in this case - title? If data is already parsed then how come when I try to access it it returns undefined?
undefinedand have data when stringified...