I am getting a string object after splitting from a text. Now I am trying to convert that text to a JSON object.
Text after Splitting
{location:"Web",initial:"",firmType:"",toaxfrtype:""}
When I am trying to parse it using JSON.parse, I am getting an error,
SyntaxError: Unexpected token l.
I have some other string value as the same in the above text. It was parsing fine using JSON.parse. Only the above string is not working.
Can anybody help me in this issue.
You need quotes " so change
{location:"Web",initial:"",firmType:"",toaxfrtype:""}
to
{"location":"Web","initial":"","firmType":"","toaxfrtype":""}
I have attached the image where it says valid now after correction. You can online validators to validate it first.
This is not JSON. You need to quote all Strings, including the keys.
Related
I am having problem converting a JSON String into Javascript object.
I ran into a few suggestions which said that I should not be using multi-line string but using single line string too did not work.
Snippet: https://jsfiddle.net/ankschoubey/hjh2d3z6/
SyntaxError: Unexpected token F in JSON at position 4536
Because you're including JSON as a JavaScript string value, you'll have to double-up on all the embedded backslash characters because they'll be parsed twice: first when the JavaScript parser reads the overall string constant to create the string value, and then again when you call JSON.parse().
Thus that portion of the string with \" should be \\". That way, the JavaScript string parsing will turn \\" into just \", and that's what will make the JSON parser happy.
Looks pretty simple but I am unable to figure it out
var str="[{name:\"House\",id:\"1\"},{name:\"House and Land\",id:\"5\"},{name:\"Land\",id:\"6\"},{name:\"Terrace\",id:\"11\"}]";
JSON.parse(str.replace(/\s/g, "").replace(/\//g, ''));
I am unable to the convert above string(which comes from 3rd party website) to valid json so that I can iterate it on my side
error
VM5304:1 Uncaught SyntaxError: Unexpected token n in JSON at position 2
at JSON.parse (<anonymous>)
JSON requires the keys to be quoted. It appears that your keys are coming in unquoted. So add another .replace statement to insert the quote back in:
.replace(/(\w+):/g, '"$1":');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
Property names must be double-quoted strings; trailing commas are forbidden.
COMPLETE SOLUTION:
.replace(/(,|{)\s*(\w+)\s*:/g, '$1"$2":');
I have a JSON data as below which getting from remote URL.
{"myitems":[{\"NAME\":\"JOHN\"},{\"NAME\":\"MICHAEL\"},{\"NAME\":\"CATTY\"},{\"NAME\":\"DAVID\"}]}
in JavaScript I want to parse
JSON.parse(mydata);
But I'm getting the error as:
Invalid Character
What can I do?
You need to fix the errors in the JSON. This fix should be done at source (i.e. you should change the report URL that is outputting invalid JSON so it outputs valid JSON instead).
Your string literals need to start and end with " (not \"). With the exception of "myitems", all of them have that error.
You have to remove slashes this will fix your issue:
Ex:
var str='{"myitems":[{\"NAME\":\"JOHN\"},{\"NAME\":\"MICHAEL\"},{\"NAME\":\"CATTY\"},{\"NAME\":\"DAVID\"}]}';
var output=JSON.parse(str.replace(/\\/g, ""));
above example will gives you output.
I am using JSON.parse to parse a json string. It works fine for standard json format string but I doesn't work for some string like below:
'{"filter":{"email":/net/},
"rejected":"[/net/,/net/]"
}'
the problem on this string is that it missing double quote on /net/ which makes JSON.parse failed. Is there a way to parse this string into json object? Or is there a library to add a double quote on the missing part of this string? I tried a few regex patterns to make up this string but can't find a good solution.
Parsing this string I get an unexpected token error, what is the unexpected token?
JSON.parse("[{"attr1":079455,"Attr2": 3},{"Attr1":847987,"Attr2": 3}]");
I keep looking here at the documentation but I'm just not seeing what's wrong with this string? I've tried all sorts of stringifying and replacing double quotes with single ect.
JSON format does not allow leading zeroes on numbers, except for the special case of 0 or floating point numbers that begin with 0.. See the diagram that shows the format of numbers at http://www.json.org/.
So the number 079455 is not valid JSON.
You should fix the program that's generating the JSON in the first place. It should use a library function to produce JSON, instead of formatting it by hand.
If you can't, you could use the following clumsy Javascript to remove the extraneous zeroes:
json_str = json_str.replace(/":0+/, '":');
As well as incorrect number formats, you are not correctly wrapping your String. If you want to include " characters inside your string, you should wrap it with ':
JSON.parse('[{"attr1":79455,"Attr2": 3},{"Attr1":847987,"Attr2": 3}]');