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
When i post data to my server , i get this error in my database " SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data[Learn More]" but anyway the data still goes into the database. What could be there error?
Most probably server returned error page (which is not valid JSON), check response in network tab in developer tools.
This is fraustating me for 5 hours and now i have to finally ask this question.
I am trying to parse JSON using Javascript, but i don't know why i am getting this error on Console:
Uncaught SyntaxError: Unexpected token (…)
(anonymous function) # VM382:2InjectedScript._evaluateOn #
VM251:904InjectedScript._evaluateAndWrap # VM251:837InjectedScript.evaluate
#VM251:693
JSON : http://pastebin.com/DddXQj6d
JS Code:
var json=**big json**;
var obj=JSON.parse(json);
Tried:
JSON.stringify(json);
json= "'" + json+ "'";
Loading JSON from URL
According to JSONLint and the Pastebin that you posted, the JSON is invalid, mostly due to the use of \'. Once you replace all of them by ', it should work normally.
In your original JSON file there are probably going to be some occurrences of \\'. As a string they become \'.
If you’re using Linux, a simple
sed -i "s/\\\\\\\'/\'/g" yourJSONFile.json
on your file fixes everything.
JSONLint says “Valid JSON” then.
You can also try
JSON.parse(json.replace(/\\'/,'\''));
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"]]'
I need to consume a service which is returning JSON. I have no influence whatsoever on that service (third party).
If I do
JSON.parse (data)
I get an
SyntaxError: Unexpected token
I know the service works because the error occurs depending on input parameters.
In other words, sometimes it definitely works! The HTTP response code is 200 so it is not some kind of access error, and it is repeatable.
Can I assume they are returning malformed JSON?
Writing the data as a text file to disk and reading it like this:
fs = require('fs')
fs.readFile 'output.json', '', (err, data) ->
if err?
console.log err
json = JSON.parse(data)
console.log json
returns
undefined:1
De L\'Embustier
^
SyntaxError: Unexpected token '
at Object.parse (native)
which is kind of strange because it seems as if the string is correctly escaped but it's nevertheless not being read correctly.
The file is 300+k; haven't seen how to attach it.
EDIT:
Response from jsonlint.com
Parse error on line 1297:
... "Address": "36 Rue De L\'Embust
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
EDIT2:
Here is the whole file then:
http://pastebin.com/ACUfvPCx
The service is indeed returning malformed JSON.
There is no \' escape sequence in JSON. It's perfectly valid to escape any character in Javascript, but JSON uses a subset of the Javascript syntax and only allows escaping characters that actually could need escaping. As an apostrophe is not used as a string delimiter in JSON, it never needs escaping.
If you can't get the service fixed, you would need to request the JSON as plain text, replace the escaped apostrophes with just the apostrophe itself, then parse the text as JSON.