Why it is showing error when parsing JSON having multiple properties? - javascript

Code:
JSON.parse('{"name":"dibya","company":"wipro}');
Error :
M208:1 Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at :1:6

It throws an error beacuse it is not a valid json. Should be:
JSON.parse('{"name":"dibya","company":"wipro"}');
You can check if it is valid on https://jsonlint.com/

Related

JSON.parse produces a SyntaxError

Inputting this:
JSON.parse("{list : []}");
produces this in the console:
VM9793:1 Uncaught SyntaxError: Unexpected token l in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:1:6
JSON requires object keys to be stings, so it needs to be:
JSON.parse('{"list" : []}');

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

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.

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"]]'

Uncaught SyntaxError: Unexpected token ILLEGAL on php json_encode

When I do this:
onClick = "return generateClient('<?php echo json_encode($_POST)?>');"
I am getting this error on the google chrome console.
Uncaught SyntaxError: Unexpected token ILLEGAL
But when I do this from php:
print_r(json_encode($_POST));
I get:
{"lang-select":"C++","question-id":"1","method-name":"Rishi","param-count":"1","lib-path":"c:\\h\\b.out","return-select":"unsigned int","sample-count":"1","class-name":"m"}
What can be the fault in this?
Your data contains " characters.
Your attribute values are delimited by " characters.
The first " in the data will end the attribute value.
A validator would have picked this up for you.
Run your data through htmlspecialchars to encode the quote marks.

Parsing simple JSON using Ext gives SyntaxError: Unexpected token ILLEGAL

i'm doing a simple parse of some JSON and it's giving me an error in chrome, what am i missing here?
Ext.util.JSON.decode("{info: {synopsis: 'test'}}");
SyntaxError: Unexpected token ILLEGAL
Thanks a lot
http://www.json.org/
Think you should use double quotes instead of single quotes.
Ext.util.JSON.decode('{"info": {"synopsis": "test"}}');
Be careful if you are using ExtJs 4 onward, You have to use
Ext.JSON.decode('{"info": {"synopsis": "test"}}');

Categories

Resources