Parsing JSON Giving "Unexpected Token O" Error

Error Uncaught SyntaxError: Unexpected token with JSON.parse

products is an object. (creating from an object literal)

JSON.parse() is used to convert a string containing JSON notation into a Javascript object.

Your code turns the object into a string (by calling .toString()) in order to try to parse it as JSON text.

The default .toString() returns "[object Object]", which is not valid JSON; hence the error.

SyntaxError: Unexpected token o in JSON at position 1

The JSON you posted looks fine, however in your code, it is most likely not a JSON string anymore, but already a JavaScript object. This means, no more parsing is necessary.

You can test this yourself, e.g. in Chrome's console:

new Object().toString()
// "[object Object]"

JSON.parse(new Object())
// Uncaught SyntaxError: Unexpected token o in JSON at position 1

JSON.parse("[object Object]")
// Uncaught SyntaxError: Unexpected token o in JSON at position 1

JSON.parse() converts the input into a string. The toString() method of JavaScript objects by default returns [object Object], resulting in the observed behavior.

Try the following instead:

var newData = userData.data.userList;

How to fix Unexpected token o in JSON at position 1 using JavaScript?

add json()
let data = res.json();

Unexpected token o in JSON at position 1 fetch request

Long story short the problem was above what i posted, i used express.json() as a middleware which was what i needed but i didn't use it properly.
Using express.json() as middleware expects the body of the request to be a string so that it can convert it into json, so in react i used JSON.stringify(jsonObject) where jsonObject was the fetch body content that i needed.

Parsing JSON giving unexpected token o error

Your data is already an object. No need to parse it. The javascript interpreter has already parsed it for you.

var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
document.write(cur_ques_details['ques_title']);

SyntaxError: Unexpected token in JSON at position 0

The wording of the error message corresponds to what you get from Google Chrome when you run JSON.parse('<...'). I know you said the server is setting Content-Type:application/json, but I am led to believe the response body is actually HTML.

Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token < in JSON at position 0"

with the line console.error(this.props.url, status, err.toString()) underlined.

The err was actually thrown within jQuery, and passed to you as a variable err. The reason that line is underlined is simply because that is where you are logging it.

I would suggest that you add to your logging. Looking at the actual xhr (XMLHttpRequest) properties to learn more about the response. Try adding console.warn(xhr.responseText) and you will most likely see the HTML that is being received.

JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0`

You have a strange char at the beginning of the file.

data.charCodeAt(0) === 65279

I would recommend:

fs.readFile('addresses.json', function (err, data) {
if (data) {
console.log("Read JSON file: " + data);
data = data.trim();
//or data = JSON.parse(JSON.stringify(data.trim()));
storage = JSON.parse(data);
}});

Unexpected token o in JSON at position 1 OR Cannot read property '0' of undefined

When using jQuery's $.ajax, it checks the content-type header returned by the server. If it's set to application/json, then it runs JSON.parse() for you.

This means your response may already be an object (and not a string that needs to be "parsed").

Also, your data is a single object, not an array of objects, there is no row property/array. You just need to use $("#id").val(response.id);, etc.



Related Topics



Leave a reply



Submit