I want to pass an email address as a query string and as such I encode it just before sending it with this line of code:
var encoded = encodeURIComponent(email).replace('.' '%2E');
Apparently the period shouldn't matter but I keep getting "can't find module 'com' " if i run it that way (I'm coding on node and using express and using a res.render() call)
Don't really understand why in my case periods are causing issues but either way this is the exact error I get:
var encoded = encodeURIComponent(email).replace('.' '%2E');
^^^^^
SyntaxError: Unexpected string
Don't really understand why in my case periods are causing issues
It's not the presence of a period. It's the lack of a comma.
var encoded = encodeURIComponent(email).replace('.', '%2E');
// ^ this here
Related
I think this is a basic question, but I can't understand the reason. in JSON, it's valid to has a special character like "asdfadf\tadfadf", but when try parse it, it's shown error.
eg. code:
let s = '{"adf":"asdf\tasdfdaf"}';
JSON.parse(s);
Error:
Uncaught SyntaxError: Bad control character in string literal in JSON at position 12
at JSON.parse (<anonymous>)
at <anonymous>:1:6
I need to understand what is the issue, and solution.
You have to take into account that \t will be involved in two parsing operations: first, when your string constant is parsed by JavaScript, and second, when you parse the JSON string with JSON.parse(). Thus you must use
let s = '{"adf":"asdf\\tasdfdaf"}';
If you don't, you'll get the error you're seeing from the actual tab character embedded in the string. JSON does not allow "control" characters to be embedded in strings.
Also, if the actual JSON content is being created in server-side code somehow, it's probably easier to skip building a string that you're immediately going to parse as JSON. Instead, have your code build a JavaScript object initializer directly:
let s = { "adf": "asdf\tasdfdaf" };
In your server environment there is likely to be a JSON tool that will allow you to take a data structure from PHP or Java or whatever and transform that to JSON, which will also work as JavaScript syntax.
let t = '{"adf":"asdf\\tasdfdaf"}';
var obj = JSON.parse(t)
console.log(obj)
I have the following data coming in from my server into my js code, from my server.
{"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxxx\"]","message": "This is a test"}]}
My code parses the above json string in the following manner.
data = '{"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxx\",\"+91xxxxxxxx\"]","message": "This is a test"}]}'
parsed = JSON.parse(data);
This throws the following exception
Uncaught SyntaxError: Unexpected token + in JSON at position 54
at JSON.parse (<anonymous>)
at eval (eval at <anonymous> (entry.html:2), <anonymous>:1:6)
at entry.html:298
I did a little bit of further digging and found the source of the json string.
Here is where the string is coming in from my python code
data = {"recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxx\"]"} # This data comes in from my database, and I can't do anything about what quotes are used.
javascript_supplied_data = json.dumps(data) #This data goes to the frontend via webhook
I tried putting the same data into a json view via this online viewer, and it didn't throw any error and displayed the data correctly.
What I can't understand is, I am doing a json.dumps in my python code, so the string response should be json parsable. So why does JSON.parse throw this error?
Is there something wrong with the json libraries at the python end or the javascript end, or am I too much of a noob?.
Please help me figure out what is causing this issue, and how to solve it.
NOTE: I don't have any control over the string that comes in from the server.
When you have valid JSON, but put it in a string literal, the escapes treated by the literal notation in JavaScript make the string different. The backslashes are interpreted as for escaping the next character.
So either you have to go through the string literal and double all the backslashes, or you can apply String.raw to the string literal as template string:
var data = String.raw`{"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxx\",\"+91xxxxxxxx\"]","message": "This is a test"}]}`;
var parsed = JSON.parse(data);
console.log(parsed);
Note however, that the JSON you posted at the start of your question is valid.
On the other hand, the error you get indicates that the \" just before the first + is interpreted as just ". This means the \ is not actually there when the string is received from the server. This is usually caused by a similar escaping problem at the server side, where the programmer intended to send the backslash to the client, but actually just escaped the " on the server, which resulted in only the " being sent to the client and not \".
You have to use single quotes here while escape sequence.
if we use double quotes for escape sequence here it will result in
"recipients": "["+91xxxxxxxx","+91xxxxxxxx"]"
double quotes inside double quotes which is why your code breaks.
data = '{"triggers": [{"message_type": "sms","recipients": "[\'+91xxxxxxxx\',\'+91xxxxxxxx\']","message": "This is a test"}]}';
parsed = JSON.parse(data);
console.log(parsed)
We are trying to get rid of all of our eval() calls in our JavaScript. Unfortunately, I am not much of a JavaScript programmer, and I need some help.
Many of our eval() calls operate on strings, outputs from a web service, that are very JSON-like, for example, we might eval the following string:
ClassMetaData['Asset$Flex'] = {
fields: {
}
,label: 'Flex Fields'
};
I've seen various suggestions on the Internet suggesting Ext.decode(). The documentation for it says - "Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set." The string that I am supplying as an argument isn't legitimate JSON as I understand it (the field names aren't quoted), but Ext.decode() nearly works for me anyway. If I decode the above string, I get an error (why?) - "Uncaught SyntaxError: Unexpected token ;". However, if I remove the trailing semi-colon, and decode, everything seems to be fine.
I am using the following code to determine whether the decode call and the eval call do the same thing:
var evaled = eval(inputString);
var decoded = Ext.decode(inputString.replace(";", "")); // remove trailing ";", if any
console.log("Equal? - " + (JSON.stringify(decoded) == JSON.stringify(evaled)));
Unfortunately, this is not a very good solution. For example, some of the input strings to eval are fairly complex. They may have all sorts of embedded characters - semicolons, HTML character encodings, etc. Decode may complain about some other syntax problem, besides semicolons at the end, and I haven't found a good way to determine where the problem is that decode objects to. (It doesn't say "illegal character in position 67", for example.)
My questions:
Could we, with a small amount of work, create a generic solution
using decode?
Is there an easy way to convert our JSON-like input
into true JSON?
Is there a better way of comparing the results of
eval and decode than JSON.stringify(decoded) == JSON.stringify(evaled)?
I'm using LZstring to do a UTF-16 compression on a string and storing it in a database. Specifically, it was a JSON.stringify'd data stored in a cordova.sqllite db. When I retrieve the data from the db there are times when it causes SyntaxError: Unexpected EOF
I'm thinking that depending on the input, LZString's compressToUTF16 function produces an invalid character. The weird thing is, it was stored in the DB properly (no unexpected EOF errors when storing into the DB).
Storing a normal string or a compressToBase64'd string does not cause an error so I don't think it's a problem with the DB.
So apparently, LZString's UTF16 compression sometimes generates \u2028 and \u2029 characters which are treated by the parser as line breaks (see here for more details). I replaced all occurrences of both unicodes after compression and before storage, and after retrieval and before decompression to fix the problem.
before storage
var dbvalue = LZString.compressToUTF16(JSON.stringify(value));
dbvalue = dbvalue.replace(/\u2028/g, '\u32800').replace(/\u2029/g, '\u32801');
after retrieval
var utfdata = dbvalue.replace(/\u32800/g,'\u2028').replace(/\u32801/g,'\u2029');
var value = JSON.parse(LZString.decompressFromUTF16(utfdata));
EDITED
The replacement character is not used by LZString and takes up less space. See here for more details
suppose I have something like
var a = '["\t"]'
If I do
eval('var result = ' + a)
Everything works fine. But if I do
var result = JSON.parse(a)
It throws error: Unexpected token.
Same happens with \b, \f: works with eval, while fails with JSON.parse. Why? Shouldn't the parser behave in the same way when encounters "\t"?
On the other hand, both eval and JSON.parse fail with a \n (as expected), but they also both fail with a \r. Why is this?
I'm a little bit confused with all this so could anybody explain what is going on? If possible with details: how is the parser behaving in the two cases?
You have to escape \ in the JavaScript string, so you'd end up with
var a = '["\\t"]'
For details, please refer to "http://json.org/"
Well that's because it's not valid JSON.
Don't try to write JSON yourself. Instead, use JSON.stringify to properly encode your data.
var json = JSON.stringify(["\t"]);
JSON.parse(json);
//=> ["\t"]