Error parsing JSON data - "Uncaught SyntaxError: Unexpected token ." - javascript

I'm getting the error below in Chrome while parsing a JSON data. The data sample is at http://jsoneditoronline.org/?id=31ffc7c0e7e1a9a2adf641306497b57a This is a valid JSON and my server is sending the correct Content-Type value (application/json).
Uncaught SyntaxError: Unexpected token .
Firefox reports a slightly different message but it all points to the presence of the period (.) in the beginning of the content.
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 6 of the JSON data
I've tried both $.parseJSON() and JSON.parse() methods.
What's the cause of this error? Please enlighten.
I've read the other similar posts here, but they refer to a different character like <, etc.,
EDIT: This is the piece of code I'm using to retrieve the server data.
$.ajax({
url : searchUrl
}).done(function(data) {
var json_array = JSON.parse(data); // Apparently data is already JSON parsed.
});

That data probably is already an object, try it without $.parseJSON() or JSON.parse() and it should work.

Related

why JSON.parse does not convert a string to json object?

I tried to convert a string to a javascript object using JSON.parse but it's not working.
I tried everything but did not get anything on the console.log and there is no error message.
JSON.parse(`{'exp': '1', 'input': '1d6404f66ed3d72e', 'iterate': 'no'}`);
Update
In the real code, I'm passing the value from an object
console.log(JSON.parse(future.onIOPub.data['text/plain']))
When you run this, you should see
Uncaught SyntaxError: Unexpected token ' in JSON at position 1
This error is because you are using single quotes. JSON only accepts double quotes, as described in the spec
https://www.json.org/json-en.html

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data - AngularJS [duplicate]

When i post data to my server , i get this error in my database " SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data[Learn More]" but anyway the data still goes into the database. What could be there error?
Most probably server returned error page (which is not valid JSON), check response in network tab in developer tools.

Unexpected Token while Parsing JSON

This is fraustating me for 5 hours and now i have to finally ask this question.
I am trying to parse JSON using Javascript, but i don't know why i am getting this error on Console:
Uncaught SyntaxError: Unexpected token (…)
(anonymous function) # VM382:2InjectedScript._evaluateOn #
VM251:904InjectedScript._evaluateAndWrap # VM251:837InjectedScript.evaluate
#VM251:693
JSON : http://pastebin.com/DddXQj6d
JS Code:
var json=**big json**;
var obj=JSON.parse(json);
Tried:
JSON.stringify(json);
json= "'" + json+ "'";
Loading JSON from URL
According to JSONLint and the Pastebin that you posted, the JSON is invalid, mostly due to the use of \'. Once you replace all of them by ', it should work normally.
In your original JSON file there are probably going to be some occurrences of \\'. As a string they become \'.
If you’re using Linux, a simple
sed -i "s/\\\\\\\'/\'/g" yourJSONFile.json
on your file fixes everything.
JSONLint says “Valid JSON” then.
You can also try
JSON.parse(json.replace(/\\'/,'\''));

JSON: Error while Parsing

I've the following JSON(Valid) sting.
[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]
it gives error when I try to Parse using the JSON.parse function
here is the code that I'm using for parsing.
JSON.parse('[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]');
and it gives an error in console Uncaught SyntaxError: Unexpected token i
here is the Correct Output by same string using online JSON viewer.
When you use JSON viewer, it's different from when you use the code in your JS code. Like #Jonathan stated, you should double escape you json sting.
JSON.parse('[["abc","{\\"icon\\":\\"adjust\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"red\\"}"],["xyz","{\\"icon\\":\\"archive\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"green\\"}"],["azs","{\\"icon\\":\\"asterisk\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"darkred\\"}"]]');
Your json structure is invalid. You should use this instead(without slashes):
'[["abc",["icon":"adjust","prefix":"fa","markerColor":"red"]],["xyz",["icon":"archive","prefix":"fa","markerColor":"green"]],["azs",["icon":"asterisk","prefix":"fa","markerColor":"darkred"]]'

read a json string which has a "'" (malformed?)

I need to consume a service which is returning JSON. I have no influence whatsoever on that service (third party).
If I do
JSON.parse (data)
I get an
SyntaxError: Unexpected token
I know the service works because the error occurs depending on input parameters.
In other words, sometimes it definitely works! The HTTP response code is 200 so it is not some kind of access error, and it is repeatable.
Can I assume they are returning malformed JSON?
Writing the data as a text file to disk and reading it like this:
fs = require('fs')
fs.readFile 'output.json', '', (err, data) ->
if err?
console.log err
json = JSON.parse(data)
console.log json
returns
undefined:1
De L\'Embustier
^
SyntaxError: Unexpected token '
at Object.parse (native)
which is kind of strange because it seems as if the string is correctly escaped but it's nevertheless not being read correctly.
The file is 300+k; haven't seen how to attach it.
EDIT:
Response from jsonlint.com
Parse error on line 1297:
... "Address": "36 Rue De L\'Embust
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
EDIT2:
Here is the whole file then:
http://pastebin.com/ACUfvPCx
The service is indeed returning malformed JSON.
There is no \' escape sequence in JSON. It's perfectly valid to escape any character in Javascript, but JSON uses a subset of the Javascript syntax and only allows escaping characters that actually could need escaping. As an apostrophe is not used as a string delimiter in JSON, it never needs escaping.
If you can't get the service fixed, you would need to request the JSON as plain text, replace the escaped apostrophes with just the apostrophe itself, then parse the text as JSON.

Categories

Resources