JSON.parse() specifically the operators - javascript
Can someone explain why when I try to use JSON.parse() on the operators "+", "-", "*", "/" it returns:
SyntaxError: Unexpected token +
SyntaxError: Unexpected token -
SyntaxError: Unexpected token *
SyntaxError: Unexpected token /
I am passing a string which is valid but it will not return the operator as itself. There is nothing in the documentation that would explain why this is invalid: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Any help is appreciated. Thank you.
Edit for clarity*
In the documentation, the cases below are all valid. They are arbitrary strings being passed to JSON.parse(). You can verify these examples in your console. My question is why passing an operator as a string will not return the operator.
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
In JSON, strings need to be in double quotes. So if you want a JSON string that contains an operator, it needs to be quoted.
var json = '"+"';
var result = JSON.parse(json);
alert(result);
This will alert +.
The single quotes are for the Javascript literal, the double quotes are part of the JSON syntax.
JSON it's a string that contains key value pairs. JSON.parse() it's a function that will return object with properties named with key, and it mean that you can't use +, -, /, * as name for object property. That's why you receive this error.
Operators are not valid json. JSON.parse is for converting a json formatted string into corressponding javascript types and objects. You are looking for eval. eval executes a string as if it were javascript code.
Information on eval
Information on valid json
Related
Unexpected token "“" (0x201C) [duplicate]
Why is it that //Code JSON.parse("{'name':'Khushal Khan'}"); results in this error //Resposnse SyntaxError: Unexpected token ' while this works perfectly //Code JSON.parse('{"name":"Khushal Khan"}'); Output: //Response Object {name: "Khushal Khan"}
The problem is the type of quote used in your JSON string, not the outer quotes. The JSON specification only allows for double quoted strings. You could use either type of quote to actually pass your JSON string to the parse() function, though. From the JSON spec:
The problem is not that your JavaScript string uses " characters but that your JSON strings do not. JSON is not JavaScript. JSON strings must be delimited by " characters. From the specification: string = quotation-mark *char quotation-mark and quotation-mark = %x22 ; "
Problem is not with double quoted string, but the json should have no single quotes as delimiters.
JSON.parse: unexpected character -- Syntax is correct, data type is string
I am completely baffled. I am getting an exception when trying to JSON.parse() the following string: {"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"} The exact error reported by the exception is: JSON.parse: unexpected character at line 1 column 1 of the JSON data The string syntax is correct according to JSONLint. I have also confirmed that the data type is string. What could be causing the exception?
Maybe you forgot to quote the string? console.log(JSON.parse('{"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}')) Omitting the quotes would yield an object literal: console.log({"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"})
You're probably assigning that value literally, wich is an object not a string, so: var invalidJSON = {"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}; var validJSON = '{"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}';
JSON.parse error is giving error with double quotes string
Why is it that //Code JSON.parse("{'name':'Khushal Khan'}"); results in this error //Resposnse SyntaxError: Unexpected token ' while this works perfectly //Code JSON.parse('{"name":"Khushal Khan"}'); Output: //Response Object {name: "Khushal Khan"}
The problem is the type of quote used in your JSON string, not the outer quotes. The JSON specification only allows for double quoted strings. You could use either type of quote to actually pass your JSON string to the parse() function, though. From the JSON spec:
The problem is not that your JavaScript string uses " characters but that your JSON strings do not. JSON is not JavaScript. JSON strings must be delimited by " characters. From the specification: string = quotation-mark *char quotation-mark and quotation-mark = %x22 ; "
Problem is not with double quoted string, but the json should have no single quotes as delimiters.
How can I parse this string into an array?
I have a string representation of the following array, generated from Signature Pad: var myData = "[{lx:47,ly:28,mx:47,my:27},{lx:47,ly:32,mx:47,my:28},{lx:47,ly:40,mx:47,my:32},{lx:48,ly:50,mx:47,my:40},{lx:49,ly:59,mx:48,my:50},{lx:49,ly:66,mx:49,my:59},{lx:51,ly:72,mx:49,my:66},{lx:54,ly:76,mx:51,my:72},{lx:56,ly:76,mx:54,my:76},{lx:58,ly:76,mx:56,my:76},{lx:59,ly:76,mx:58,my:76},{lx:61,ly:76,mx:59,my:76},{lx:62,ly:76,mx:61,my:76},{lx:64,ly:76,mx:62,my:76},{lx:66,ly:73,mx:64,my:76},{lx:70,ly:69,mx:66,my:73},{lx:73,ly:64,mx:70,my:69},{lx:75,ly:61,mx:73,my:64},{lx:79,ly:56,mx:75,my:61},{lx:82,ly:51,mx:79,my:56},{lx:84,ly:46,mx:82,my:51},{lx:85,ly:43,mx:84,my:46},{lx:87,ly:40,mx:85,my:43},{lx:88,ly:35,mx:87,my:40},{lx:90,ly:34,mx:88,my:35},{lx:92,ly:33,mx:90,my:34},{lx:93,ly:32,mx:92,my:33},{lx:94,ly:32,mx:93,my:32},{lx:96,ly:33,mx:94,my:32},{lx:96,ly:35,mx:96,my:33},{lx:99,ly:37,mx:96,my:35},{lx:101,ly:42,mx:99,my:37},{lx:101,ly:46,mx:101,my:42},{lx:101,ly:50,mx:101,my:46},{lx:101,ly:54,mx:101,my:50},{lx:102,ly:57,mx:101,my:54},{lx:104,ly:58,mx:102,my:57},{lx:105,ly:59,mx:104,my:58},{lx:107,ly:60,mx:105,my:59},{lx:108,ly:60,mx:107,my:60},{lx:109,ly:60,mx:108,my:60},{lx:110,ly:60,mx:109,my:60},{lx:112,ly:58,mx:110,my:60},{lx:114,ly:57,mx:112,my:58},{lx:116,ly:54,mx:114,my:57},{lx:119,ly:53,mx:116,my:54},{lx:120,ly:50,mx:119,my:53},{lx:123,ly:49,mx:120,my:50},{lx:127,ly:48,mx:123,my:49},{lx:130,ly:48,mx:127,my:48},{lx:132,ly:48,mx:130,my:48},{lx:134,ly:49,mx:132,my:48},{lx:136,ly:50,mx:134,my:49},{lx:137,ly:52,mx:136,my:50},{lx:139,ly:56,mx:137,my:52},{lx:140,ly:59,mx:139,my:56},{lx:140,ly:60,mx:140,my:59},{lx:143,ly:61,mx:140,my:60},{lx:144,ly:61,mx:143,my:61},{lx:146,ly:61,mx:144,my:61},{lx:151,ly:61,mx:146,my:61},{lx:156,ly:61,mx:151,my:61},{lx:161,ly:61,mx:156,my:61},{lx:167,ly:60,mx:161,my:61},{lx:173,ly:60,mx:167,my:60},{lx:178,ly:60,mx:173,my:60},{lx:185,ly:60,mx:178,my:60},{lx:192,ly:60,mx:185,my:60},{lx:198,ly:60,mx:192,my:60},{lx:207,ly:60,mx:198,my:60},{lx:214,ly:60,mx:207,my:60},{lx:221,ly:60,mx:214,my:60},{lx:226,ly:60,mx:221,my:60},{lx:229,ly:60,mx:226,my:60},{lx:233,ly:60,mx:229,my:60},{lx:234,ly:60,mx:233,my:60},{lx:235,ly:60,mx:234,my:60},{lx:237,ly:60,mx:235,my:60},{lx:238,ly:60,mx:237,my:60},{lx:239,ly:60,mx:238,my:60},{lx:241,ly:60,mx:239,my:60},{lx:244,ly:60,mx:241,my:60},{lx:245,ly:60,mx:244,my:60},{lx:246,ly:60,mx:245,my:60},{lx:248,ly:59,mx:246,my:60},{lx:248,ly:58,mx:248,my:59},{lx:248,ly:57,mx:248,my:58}]"; I tried to convert it to an array: JSON.parse(myData); But I get the following error message: SyntaxError: Unexpected token l What am I doing wrong? I can't see the mistake:( I tried pasting the exact same data without the quotes in the console and it creates an array: var myData = [{lx:47,ly:28,mx:47,my:27},{lx:47,ly:32,mx:47,my:28},{lx:47,ly:40,mx:47,my:32},{lx:48,ly:50,mx:47,my:40},{lx:49,ly:59,mx:48,my:50},{lx:49,ly:66,mx:49,my:59},{lx:51,ly:72,mx:49,my:66},{lx:54,ly:76,mx:51,my:72},{lx:56,ly:76,mx:54,my:76},{lx:58,ly:76,mx:56,my:76},{lx:59,ly:76,mx:58,my:76},{lx:61,ly:76,mx:59,my:76},{lx:62,ly:76,mx:61,my:76},{lx:64,ly:76,mx:62,my:76},{lx:66,ly:73,mx:64,my:76},{lx:70,ly:69,mx:66,my:73},{lx:73,ly:64,mx:70,my:69},{lx:75,ly:61,mx:73,my:64},{lx:79,ly:56,mx:75,my:61},{lx:82,ly:51,mx:79,my:56},{lx:84,ly:46,mx:82,my:51},{lx:85,ly:43,mx:84,my:46},{lx:87,ly:40,mx:85,my:43},{lx:88,ly:35,mx:87,my:40},{lx:90,ly:34,mx:88,my:35},{lx:92,ly:33,mx:90,my:34},{lx:93,ly:32,mx:92,my:33},{lx:94,ly:32,mx:93,my:32},{lx:96,ly:33,mx:94,my:32},{lx:96,ly:35,mx:96,my:33},{lx:99,ly:37,mx:96,my:35},{lx:101,ly:42,mx:99,my:37},{lx:101,ly:46,mx:101,my:42},{lx:101,ly:50,mx:101,my:46},{lx:101,ly:54,mx:101,my:50},{lx:102,ly:57,mx:101,my:54},{lx:104,ly:58,mx:102,my:57},{lx:105,ly:59,mx:104,my:58},{lx:107,ly:60,mx:105,my:59},{lx:108,ly:60,mx:107,my:60},{lx:109,ly:60,mx:108,my:60},{lx:110,ly:60,mx:109,my:60},{lx:112,ly:58,mx:110,my:60},{lx:114,ly:57,mx:112,my:58},{lx:116,ly:54,mx:114,my:57},{lx:119,ly:53,mx:116,my:54},{lx:120,ly:50,mx:119,my:53},{lx:123,ly:49,mx:120,my:50},{lx:127,ly:48,mx:123,my:49},{lx:130,ly:48,mx:127,my:48},{lx:132,ly:48,mx:130,my:48},{lx:134,ly:49,mx:132,my:48},{lx:136,ly:50,mx:134,my:49},{lx:137,ly:52,mx:136,my:50},{lx:139,ly:56,mx:137,my:52},{lx:140,ly:59,mx:139,my:56},{lx:140,ly:60,mx:140,my:59},{lx:143,ly:61,mx:140,my:60},{lx:144,ly:61,mx:143,my:61},{lx:146,ly:61,mx:144,my:61},{lx:151,ly:61,mx:146,my:61},{lx:156,ly:61,mx:151,my:61},{lx:161,ly:61,mx:156,my:61},{lx:167,ly:60,mx:161,my:61},{lx:173,ly:60,mx:167,my:60},{lx:178,ly:60,mx:173,my:60},{lx:185,ly:60,mx:178,my:60},{lx:192,ly:60,mx:185,my:60},{lx:198,ly:60,mx:192,my:60},{lx:207,ly:60,mx:198,my:60},{lx:214,ly:60,mx:207,my:60},{lx:221,ly:60,mx:214,my:60},{lx:226,ly:60,mx:221,my:60},{lx:229,ly:60,mx:226,my:60},{lx:233,ly:60,mx:229,my:60},{lx:234,ly:60,mx:233,my:60},{lx:235,ly:60,mx:234,my:60},{lx:237,ly:60,mx:235,my:60},{lx:238,ly:60,mx:237,my:60},{lx:239,ly:60,mx:238,my:60},{lx:241,ly:60,mx:239,my:60},{lx:244,ly:60,mx:241,my:60},{lx:245,ly:60,mx:244,my:60},{lx:246,ly:60,mx:245,my:60},{lx:248,ly:59,mx:246,my:60},{lx:248,ly:58,mx:248,my:59},{lx:248,ly:57,mx:248,my:58}];
Strings are required here. Therefore use "lx" instead of lx var myData = '[{"lx":47,"ly":28 // and so on
The JSON specification is more strict than JavaScript itself. Specifically, JSON requires all object keys to be enclosed in double quotes. In the JSON spec, an object key must be a string token, and a string token begins and ends with a double quote character. Thus, you'll need to quote your object keys: var myData = '[{"lx":47,"ly":28,"mx":47,"my":27},...' EDIT: According to the Signature Pad API, you can use getSignatureString() to serialize your pad into a valid JSON string.
Your json is not valid. Object property names should be a string so you need to use quotes var myData = '[{"lx":47,"ly":28 ....} Visit json.org for more information about this topic
The fact, that your string contains a valid javascript object (note: not JSON object) lets you take advantage of the evil eval function. Just evaluate your string and store its value in a variable like this: var myData = eval("[{lx:47,ly:28,mx:47,my:27}]"); I made a fiddle for you to see that it works with the string you provided here
the variable myData has incorrect JSON format, to able to use JSON parse you need to add double quotes like here: var myData = '[{"lx":47,"ly":28,"mx":47,"my":27},...]';
JSON array syntax. Are the curly brackets (object syntax) strictly necessary?
Is it possible for a json string to include only square brackets ? For ex. : [["state","accepted"],["r_by_uid",1]] I get unexpected character error from parsing that string ... (long time since i worked on this script and i think it worked before ) . Parsing the json string will allways make an object from the string ? or is it possible to parse the string into an array ? Basically i just want to parse the string into an array , not an object . I googled some examples but couldnt find any example that is using only square brackets. As requested here is the tag that holds the json string : <button data-fproc='[["state","accepted"],["r_by_uid","1"]]' class="request_state_button">
Curly brackets are not strictly necessary. [["state","accepted"],["r_by_uid",1]] is valid JSON. A JSON text can be an object or an array. See http://json.org/ and the JSON Grammar section in https://www.ietf.org/rfc/rfc4627.txt You can validate your JSON at http://jsonlint.com/ In Javascript, JSON.parse() returns an array: JSON.parse('[["state","accepted"],["r_by_uid",1]]') // result [["state", "accepted"], ["r_by_uid", 1]] Notice that Arrays are also objects in Javascript. It works with jQuery.parseJSON() too: jQuery.parseJSON('[["state","accepted"],["r_by_uid",1]]') // result [["state", "accepted"], ["r_by_uid", 1]]
Probably this will explain your problem: var aAsArray = [["state","accepted"],["r_by_uid",1]]; var aAsString = '[["state","accepted"],["r_by_uid",1]]'; JSON.parse(aAsArray); //Uncaught SyntaxError: Unexpected token .... JSON.parse(aAsString);