JavaScript JSON parsing error - Unexpected token o in JSON at position 1 json parsing error redshift,json parsing error 8001,json parsing

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?

Answer:1

If your data has the format:

[
  {
    "title": "Example 1",
    "url": "http:\/\/www.example1.com\/"
  },
  {
    "title": "Example 2",
    "url": "http:\/\/www.example2.com\/"
  },
...

To print each title/url, you need to iterate through the result (using a for or calling forEach as below):

$.ajax({
  type: "GET",
  dataType: 'json',
  url: "https://api.myjson.com/bins/1czpnp",
  success: function(data) {
    console.log(data.title); // undefined
    console.log(data); // the [{...}] object

    // to print the title/urls, iterate over the array
    // alternative 1 (ordinary for):
    for(var i = 0; i < data.length; i++) {
      var d = data[i];
      console.log('title: ' + d.title + ' ~ url: ' + d.url);
    };

    // alternative 2 (forEach):
    data.forEach(function (d) {
      console.log('title: ' + d.title + ' ~ url: ' + d.url);
    });
  },
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Answer:2

My code resembles this: router.post("/", (req, res, next) => { foo() .then((result) => { res.send(result) }) .catch((error) => { cosnole.log(error) }) //I only want the bar() ...

My code resembles this: router.post("/", (req, res, next) => { foo() .then((result) => { res.send(result) }) .catch((error) => { cosnole.log(error) }) //I only want the bar() ...

I'm looking for maven config to minify js/css files for a spring boot application. For a normal java web app the following config works <plugins> <plugin> ...

I'm looking for maven config to minify js/css files for a spring boot application. For a normal java web app the following config works <plugins> <plugin> ...

  1. spring boot minify javascript
  2. spring boot minify html
  3. spring boot minify css
  4. spring boot minify
  5. spring boot minify-maven-plugin
  6. spring boot minification
  7. spring boot thymeleaf minify
  8. spring boot json minify
  9. spring boot maven minify

In my php code, I have two dropdowns which is the other one depends on the first one. Here is the first dropdown: <select class="form-control style" id="sel" > <option style="color: ...

In my php code, I have two dropdowns which is the other one depends on the first one. Here is the first dropdown: <select class="form-control style" id="sel" > <option style="color: ...

  1. found using wooded kingdom art
  2. found using rox spray underpass
  3. found using bunker jonesy
  4. found using cap kingdom art
  5. found using argument-dependent lookup
  6. found using seaside kingdom art
  7. found using bot spray
  8. found using bunker basher
  9. found using mushroom kingdom art
  10. found using vega outfit
  11. found using bowser kingdom art
  12. found using moon kingdom art
  13. found using rock love spray
  14. found using sunbird spray
  15. found using words
  16. command not found when using sudo

I need to create an empty JQuery element to be the outer scope list of a loop, for example : const forms_data = ['Name', 'Email']; const form = $(); for (const input_name of forms_data){ form....

I need to create an empty JQuery element to be the outer scope list of a loop, for example : const forms_data = ['Name', 'Email']; const form = $(); for (const input_name of forms_data){ form....

  1. create empty jquery object
  2. create empty jquery array
  3. create empty jquery collection
  4. create empty jquery element
  5. create empty jquery
  6. create empty div jquery
  7. create empty datatable jquery
  8. create empty list jquery
  9. jquery create empty json object
  10. jquery create empty array and push
  11. jquery create empty dom element
  12. jquery create empty selector
  13. jquery create empty variable
  14. jquery create empty selection
  15. jquery create empty object array
  16. jquery create empty html element
  17. jquery create empty promise
  18. jquery create empty json
  19. jquery create empty guid
  20. jquery create empty attribute