JSON.parse produces a SyntaxError - javascript

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" : []}');

Related

Uncaught SyntaxError: Expected ':' after property name in JSON (double quote in the string)

I have this trouble on this json array and i can't parse it:
JSON.parse("[{\"<\":\"<\"},{\">\":\">\"},{\"&\":\"&\"},{\"'\":\"'\"},{\"\"\":\""\"}]")
VM1622:1 Uncaught SyntaxError: Expected ':' after property name in JSON at position 59
at JSON.parse (<anonymous>)
at <anonymous>:1:6
I found that the problem is the last block : {""":"""}. Removed it, it's solve but i have to use it absolutely..
How to solve this?
I can't find the right escaping..
Thanks

Javascript - Unable to read json key values - Unexpected token i in JSON at position 1 - sql query output problem

I'ved got this string generated from sql query (basically using ARRAY_AGG and some cases to format the output) that looks like this
{id:2,name_of_division:'super department1',attendance_radius:1000}
However, in javascript, I cant get any of the key values and its also causing JSON parse error.
I'ved tried validating this JSON and it looks perfectly fine.
Here are some of the troubleshooting, I have no idea what is causing it
console.log(clean_div_array_var[i].toString());
output ==> {id:2,name_of_division:'super department1',attendance_radius:1000}
console.log("id:" + clean_div_array_var[i].id);
output ==> id:undefined
console.log("name_of_division:" + clean_div_array_var[i].name_of_division);
output ==> name_of_division:undefined
JSON.parse(clean_div_array_var[i]);
output ==>
VM2561:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
at JSON.parse (<anonymous>)
at get_all_key_of_div_array ((index):817)
at render ((index):2321)
at jquery.dataTables.min.js:18
at Object.b.fnGetData (jquery.dataTables.min.js:12)
at B (jquery.dataTables.min.js:17)
at Ha (jquery.dataTables.min.js:25)
at O (jquery.dataTables.min.js:16)
at jquery.dataTables.min.js:49
at i (jquery.dataTables.min.js:35)
I even try to stringify, then somehow remove quotes (I dont understand why this even appears) and then JSON parse. Same problem.
stringifyvar = JSON.stringify(clean_div_array_var[i])
output ==> "{id:2,name_of_division:'super department1',attendance_radius:1000}"
stringifyvar = stringifyvar.split('\"').join('');
console.log("stringify remove quote:" + stringifyvar);
output ==> stringify remove quote:{id:2,name_of_division:'super department1',attendance_radius:1000}
parsed_json_data = JSON.parse(stringifyvar);
output ==>
VM2503:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
at JSON.parse (<anonymous>)
at get_all_key_of_div_array ((index):816)
at render ((index):2320)
at jquery.dataTables.min.js:18
at Object.b.fnGetData (jquery.dataTables.min.js:12)
at B (jquery.dataTables.min.js:17)
at Ha (jquery.dataTables.min.js:25)
at O (jquery.dataTables.min.js:16)
at jquery.dataTables.min.js:49
at i (jquery.dataTables.min.js:35)
How can I read any of the keys properly from this json
Your result is not valid JSON. Valid format would be:
{"id":2, "name_of_division":"super department1", "attendance_radius":1000}
The keys have to be quoted, and the quotes around the string super department1 have to be double quotes, not singlequotes.
You need to generate proper JSON if you want to be able to use JSON.parse() to process it.

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

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

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/

Javascript Syntax error: Unexpected token ILLEGAL

When i use the javascript code ( http://jsfiddle.net/TRyhK/ ) :
var test = "#[]
#[]";
I reiceive a strange error: SyntaxError: Unexpected token ILLEGAL.
Does anyone know how to fix it?
Thanks
Line feeds in string literals can be expressed with \n:
var test = "#[]\n#[]";

Categories

Resources