javascript unexpected identifier after string has been escaped? - javascript

I'm trying to pass this escaped string to a function but keep getting an unexpected string error, with a similar string I'm getting an unexpected identifier error also. Does anyone know why this might be?
Video

I'm not certain, but I think %XX escapes are parsed into their original characters before JavaScript gets a hold of the string to execute.
Consider something like this instead:
<a href="javascript:;" onClick="loadPlayer('1','Disclosure%20.......');">

Related

JSON.parse string with unexpected token

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}]');

Unexpected error while parsing JSON in javascript

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.

Why can't Javascript parse this JSON array from a string literal?

What I am trying to do is simple. Parse this array holding json objects into a Javascript array.
var merchantsJson = JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]');
But the unicode character \u003C seems to be breaking the parser. In the chrome console I see "Uncaught SyntaxError: Unexpected token <"
A little more info. The above is what the code is evaluated to. In reality the code contains a jsp expression.
var merchantsJson = JSON.parse('${jsonArr}');
If I remove the single quotes, there is no issue, but eclipse give me an "missing semicolon" error message. Is it possible to parse the array with the quotes as I am trying to do?
The interpolation of ${jsonArr} is already a JavaScript object. When you wrap it in '${jsonArr}' this turns it into a string and you have to use JSON.parse.
There's no need to make it a string. You can just do var merchantsArray = ${jsonArr}. JSON constructs are already interoperable with JavaScript code.
Because there's an extra " in your string literal that is encoded by \u0022:
> '[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]'
[{"id":61693,"name":"Más"},{"id":61690,"name":"'"</div>"}]
In short, your JSON in the string is invalid. You would need to escape the unicode escape sequences for the quotes in the string literal ("'\u0022</div>"), by using
JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\u0022\u003C/div\u003E"}]'
// ^
or escape the quote character ("'\"</div>"):
JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\\u0022\u003C/div\u003E"}]');
// ^^
However, there actually is no need to use JSON at all. Just output a JS array literal into your code:
var merchantsJson = ${jsonArr};
Try to replace \u with \\u. If you don't, JSON parser receives already decoded Unicode, which created polluted JSON.
It's not because of \u003C, rather the \u0022 character is causing the issue, since it's a quotation mark and JavaScript treats it literally ending the string.
You need to escape that character: \\u0022 .
you have to use special character in your JSON string, you can escape it using \ character.
you need to replace \ with \\.
[{\"id\":61693,\"name\":\"Más\"},{\"id\":61690,\"name\":\"\\u0027\\u0022\\u003C/div\\u003E\"}]

Why doesn't my attempt to escape quotation marks in JSON work?

I am parsing a JSON-string with the JQuery.parseJSON function, as I have done lot's of times in my code. On this particular case, though, I get: Uncaught SyntaxError: Unexpected token R. The only upper case R that exists, in my JSON-formatted String, comes right after an escaped quotation mark, ... \"R ... like this. It seems like too much of a coincidence to be caused by anything other than this, but as far as I can tell, I have completely followed the proper syntax as described on json.org.
EDIT:
I've tried to manually remove the occurrances of \" in a hardcoded test, and the string formats perfectly into a proper Javascript object. In other words, my \" is definitely the problem here...
var myObject = $.parseJSON(myString);
EDIT 2:
the problematic area of my String is here displayed, both in working, and not working condition. first the problematic one:
{"lineID":33,"boxID":10,"title":"My text with the \"Ruining Part\""}
Then the working one:
{"lineID":33,"boxID":10,"title":"My text with the Ruining Part"}
Finally how i format my javabean object into JSON string.
String jsonObjectAsString = new Gson().toJson(myJavaBeanObject);
You probably need to escape the backslash in your string, if it is hardcoded, so that the final string that gets parsed has a single backslash followed by a double quote. Otherwise, the browser thinks you are trying to escape a double quote in your string, which does nothing.
So change your string to:
...\\"R...

jsonPath and unexpected illegal token

When passing the following string to jsonPath to filter a collection of objects:
$[?(en|**|(#.object.property.one=='other') && (#.object.property.two=='something(abc/def)'))]
I receive the following error:
jsonPath: Unexpected token ILLEGAL: (_v.object.property.one=='other') && (_v.object.property.two=='something(abc/def))
My initial guess is that the illegal character has something to do with round brackets or a forward slash present within a literal value. This might explain why a closing single quote around the last literal is missing. I have tried escaping both the round brackets and forward slash but to no avail. What would cause the filter method to throw the above exception?
Having a quick read over the jsonPath documentation it looks like brackets don't do the job you'd think.
In XPath, brackets are used to do groupings however in jsonPath they're used for script expressions (using whatever the underlying script engine is).
It could be that the value you're presenting as the script expression is invalid.

Categories