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}];
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}];
Share
Improve this question
asked Sep 19, 2012 at 13:58
ZestyZesty
3,00110 gold badges41 silver badges71 bronze badges
3
- 4 JSON keys must be in the form of strings, so you have invalid JSON. How do you expect it to be parsed if invalid? – Ian Commented Sep 19, 2012 at 14:01
- @Zesty Just curious, how are you getting this string? – apsillers Commented Sep 19, 2012 at 14:16
- Did you google this? This question gets asked every day on here; surely there's an answer in there somewhere. – Dexygen Commented Sep 19, 2012 at 14:18
5 Answers
Reset to default 10Strings 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 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},...]';