Just playing with JavaScript and I got an error an Uncaught SyntaxError: Unexpected token D in JSON at position 0
at JSON.parse ()
at app.js:22
The code in my JavaScript file error line is:
if (data) {
LIST = JSON.parse("TODO");
} else {
}
JSON.parse() will only work on strings or string variables that represent a JSON object, like
"
'item1': 50,
'item2': 80,
'item3': 100
"
in order to parse them into JSON objects
As you can tell, "TODO" cannot be parsed into a JSON object, but perhaps in your code there is a variable data which is a string representing JSON object. Most likely you will need to change that line to:
LIST = JSON.parse(data);
You are using json.parse on wrong data type.json.parse is made for objects while you are passing a string JSON.parse("TODO"). Note that "TODO" is a string and not an object.
Follow this example:
const todo = '{"name":"Empty the trash"}'
const obj = JSON.parse(todo);
I'm using Editor.js which outputs data as JSON and save that as String in DynamoDB. When I query that data, I want to convert that back to an Object.
Converting the string with JSON.parse() gives me Error: Unexpected token t in JSON at position 1 message.
var json = '{time=1558311121067, blocks=[{type=paragraph, data={text=writing something first}}], version=2.13.0}';
obj = JSON.parse(json);
Not sure what this error message means.
I will suggest to correct the JSON from the origin itself if you can,
if you can't than you need to replace = with : and than stringify and parse
({[^=]+|,[^=]+)=
| |_________ Replaces `=` which is preceded by `,`
|_________________ Replaces `=` which is preceded by `{`
let json = '{time=1558311121067, blocks=[{type=paragraph, data={text=writing something first}}], version=2.13.0}';
json = json.replace(/({[^=]+|,[^=]+)=/g,"$1"+':')
let obj = JSON.parse(JSON.stringify(json));
console.log(obj)
On side note:- This is code is considering above given example data, it can be updated based on the kind of values your JSON can have
Trying to parse the JSON content from AWS SQS.
First converting a string to JSON String and then to JSON Object. What is the correct way to handle this scenario ?
<script>
// JSON from SQS
var x='{"Message":"{\"default\":{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\"}","Timestamp":"2018-03-20T03:21:32.136Z"}';
x1=JSON.stringify(x);
var obj = JSON.parse(x1);
console.log(obj.Message); // undefined
alert(obj["Message"]); // undefined
</script>
I have absolutely no idea why you are trying to JSON.stringify() a string. It's already a string!
The string you have got is not valid JSON either and needs a few extra \\s in it. Where did you get it from? Or was it a typo.
var x='{"Message":"{\\\"default\\\":{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\\\"}","Timestamp":"2018-03-20T03:21:32.136Z"}';
^__________^_____________________________________________________________^
Just parse the JSON you do have then realise that obj.Message is just more JSON that could be JSON.parse()d
// JSON
var x = '{"Message":"{\\\"default\\\":{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\\\"}","Timestamp":"2018-03-20T03:21:32.136Z"}';
//Parse JSON
var obj = JSON.parse(x);
console.log(obj.Message); // string formatted as yet more JSON
The string is not right. It should be like
var x='{"Message":"{\\\"default\\\":{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\\\"}","Timestamp":"2018-03-20T03:21:32.136Z"}';
You are stringifying the x, which is already string
x1=JSON.stringify(x); //Not ok
I have a javascript object that contains a property with values of another json stringified object. I cannot get the printout version of the string back to object form. See the following code. the console.log output content of json string is exactly as right side of jsonFromStr. However, JSON.parse(json) is OK, while JSON.parse(jsonFromStr) is error. What is wrong here?
jsfiddle: http://jsfiddle.net/jma7889/qtmmpj2t/
level2Obj = { key2a: "foo", key2b: 3};
level2ObjJson = JSON.stringify(level2Obj);
level1Obj = {key1a: "bar", key1b: {level2ObjJson}};
json = JSON.stringify(level1Obj);
jsonFromStr = '{"key1a":"bar","key1b":{"level2ObjJson":"{\"key2a\":\"foo\",\"key2b\":3}"}}'; // same than json
objFromStrify = JSON.parse(json); // OK
objFromAssignedString = JSON.parse(jsonFromStr); // Uncaught SyntaxError: Unexpected token l in JSON at position 45
If you do this it will work:
jsonFromStr = '{"key1a":"bar","key1b":{"level2ObjJson":"{\\\"key2a\\\":\\\"foo\\\",\\\"key2b\\\":3}"}}';
The reason that your version does not work is that the escape sequence \" is resolved at that very moment, and so the actual value of jsonFromStr will be:
'{"key1a":"bar","key1b":{"level2ObjJson":"{"key2a":"foo","key2b":3}"}}'
... which is invalid JSON.
You need to keep those escapes unresolved in the JSON string, and this you do by escaping the escape sequence itself with additional slashes.
jsonFromStr = '{"key1a":"bar","key1b":{"level2ObjJson":{\"key2a\":\"foo\",\"key2b\":3}}}';
You have an extra "" for value of level2ObjJson key,replace jsonFromStr in your code with the above code .
I have the following line of code:
data = jQuery.parseJSON(data);
Which is creating the following error...
SyntaxError: JSON Parse error: Unexpected EOF
I can't work out how to identify what's causing the error though.
Any ideas?
Here's the data...
{
"element_type": "paint",
"edit_element_id": "2117",
"paint_id": "15",
"paint_editable_data": "zwibbler3.[{\"id\":0,\"type\":\"GroupNode\",\"fillStyle\":\"#cccccc\",\"strokeStyle\":\"#000000\",\"lineWidth\":2,\"shadow\":false,\"matrix\":[1,0,0,1,0,0],\"layer\":\"default\"},{\"id\":1,\"type\":\"PageNode\",\"parent\":0,\"fillStyle\":\"#cccccc\",\"strokeStyle\":\"#000000\",\"lineWidth\":2,\"shadow\":false,\"matrix\":[1,0,0,1,0,0],\"layer\":\"default\"},{\"id\":2,\"type\":\"BrushNode\",\"parent\":1,\"fillStyle\":\"#cccccc\",\"strokeStyle\":\"#000000\",\"lineWidth\":10,\"shadow\":false,\"matrix\":[1,0,0,1,0,0],\"layer\":\"default\",\"points\":[17,21,17,22,17,24,17,28,18,34,37,55,49,59,70,61,89,56,92,55,93,54]},{\"id\":3,\"type\":\"PathNode\",\"parent\":1,\"fillStyle\":\"#e0e0e0\",\"strokeStyle\":\"#000000\",\"lineWidth\":2,\"shadow\":false,\"matrix\":[1,0,0,1,-14,-85],\"layer\":\"default\",\"textFillStyle\":\"#000000\",\"fontName\":\"Arial\",\"fontSize\":20,\"dashes\":\"\",\"smoothness\":0.3,\"sloppiness\":0,\"closed\":true,\"arrowSize\":0,\"arrowStyle\":\"simple\",\"doubleArrow\":false,\"text\":\"\",\"roundRadius\":0,\"commands\":[0,150,100,6,200,150,200,100,6,150,200,200,200,6,100,150,100,200,6,150,100,100,100,7],\"seed\":36934}]",
"paint_image_data": "data:image/png;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AndUKiTh8hehz7WGuHV4NXAJ6Ysw5auPGjVpn9u67+Una8BUMz6phw4bpe/nQwccAfwIcWU8tDh/cvrMV5q0DuuP1tZpcBssPHrdp00aFW23POYgZM2ao0aNHq2+++YalchxIAZYCjok4vGNUuqboNDTPBurhp74NNwhds2aN1q6/9957XUvUS8Xbtm1Tw4cP19YUBb6nyvz2Bf7jtB0yj+00o+7qewzq3wMaMZmUlBRtiXHDhg15GnLCz9FMnDhRrVq1Srf9KA5GAmv0C06H4vBOM+q+vhgk8SLwEhAbHR2t0tLSVEZGhqpdu7b7qTuQQl5enpo2bZpasGCBvv8jB9hfB94AtLewHUimVBXi8KXSEhIXE2DleOBpICo2NlYNGjRIpaenB63j09G5b8z8+fP1SSSOSM0FMgE23VwXcXjXKXY9gWZIgY7/BFOKiYlRqampavDgwapFixa85Lvs3btXzZs3T2VlZenbaNAmvqFER9/HE69EHN4rpt1PpzWSeBXoB2jlmpSUpHr16qWhZcuW7ltgSIHLAbhEgjB8SY9Di5xj+CtgeeGXQb3tQ3F429QF7YPc/2YE8AxQR7eS7fsOHTpoE1gMGzXS+r367QqHHD/fsmWLys3N1UI2XwzCV/A48jITOGK47vmhOLznlHuWYDRSegTgMlriPqBQ2Obn4rT4+HgtLH6sn/OBCxcuKG5jp8N4rh8HFnYV6sfBSWBFAJ8j5AZJvos4vO9F4JkBXJDTFegWCJ1einkeenMC2IAwKDfEF4dHyUSo3IN81w+gnuFYv6aHpOdsKThX7BrfQBIRBoSBYGLg/9jYtJnVySp3AAAAAElFTkSuQmCC"
}
Is it the size that's the problem maybe?
It looks like you are trying to convert a text string into a JSON data object.
You should probably be doing the following:
instead of using jQuery.parseJSON(data), rather just use JSON.parse(data).
The error you mentioned indicates you are passing in an empty string "" as your value of data. So check the value that is being passed in to the parse method.
The data object you mentioned that you are passing in will always result in an error since it's already in a JSON format.
The parse method is intended to do the following conversion.
JSON.parse("{\"hello\":\"world\"}") returns {hello:'world'}
If you are trying to convert in an object into a string, you can try this:
JSON.stringify({hello:'world'}) returns "{\"hello\":\"world\"}"
The reason is simple. When you define the JSON in the script, the double quotes should be represented by \" , not " .
In my case i was also facing that same error in React-native but after look into my code i found that .. .my API hit URL was not complete .. and after that.. my problem was resolved :)
Maybe you can do with the eval method :
if (data !== null) {
var json_obj= eval("("+data+")"); //jQuery.parseJSON(data);
//just for check the the keys
var keys = Object.keys(json_obj);
for (var i = 0; i < keys.length; i++) {
console.log(keys[i]);
};
}