javascript eval json with base64 encoded field - javascript

I am using sun.misc.BASE64Encoder to encode an encrypted value, which is then added to a JSON field and subsequently sent to the client. I use Javascript's eval() function on the client to create an object from the JSON code. When eval() runs, it gives the error:
unterminated string literal
There are other fields in the JSON code, but I've narrowed the error specifically to the base64 encoded field. Here's the offending line of javascript code:
var result = eval( '(' + xhr.responseText + ')' );
Here's the JSON object from the Servlet:
{
'resource':'resource?Signature=j79r/2Hly+HqhS/6fdd+prfsR+kUNijUvDN0QJ14ZR43gzYScOMDypt/crks/CEphTUXVptJvSol
1ZOOvScCUhNOCb7dZk/3MKnI5tOewSACXK32/OJNd8hYpZtSTn+WhA6+f9BUIUZWA83U8Cud/Tb8V
R1yQWbDGG/mM/NiUSiY=',
'url':'http://somesite.com/pr'
}
I'm not sure why eval is dying, but it seems the value of the 'resource' JSON field contains something it doesn't care for.
Thanks in advance.
Tim

I think it may be because your JSON appears to have line breaks in it. If you remove them, does it work?

Related

JSON.parse : Bad control character in string literal

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)

How can I resolve JSON parsing error 'JSON.parse: bad control character in string literal'?

In NodeJS Backend, I send my data to client as:-
res.end(filex.replace("<userdata>", JSON.stringify({name:user.name, uid:user._id, profile:user.profile}) ))
//No error here and Object is stringified perfectly
//user is object returned in mongoDB's result
The JSON string looks like this:
{"name":"Rishavolva","uid":"5f3ce234fd83024334050872","profile":{"pic":{"small_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ4fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0OH1dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.9NkGnEumn4JW8IN0KFgxgN_6_4wN8qOgezNTyzz9osY","big_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ3fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0N31dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.yxQ1GrhLsWPn8Qwu42EfTDXqaYwFtrM6f_7cAH2eLRY"},"aboutme":"I am Rishav Bhowmik\r\nand this is navratna pulaow"}}
and that UID is just a mongodb's primary key as string, and other two base 64 strings are just JWT tokens.
Now, when this JSON string reaches the Browser, I parse it with simple:
JSON.parse(`<userdata>`)
//remember I used filex.replace("<userdata>", JSON.stringify...) in the server
For reference, my MongoDB Document here is:
Now when JSON.parse is executed on the JSON string it will look like this on final JS code.
JSON.parse(`{"name":"Rishavolva","uid":"5f3ce234fd83024334050872","profile":{"pic":{"small_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ4fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0OH1dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.9NkGnEumn4JW8IN0KFgxgN_6_4wN8qOgezNTyzz9osY","big_link":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXBsaWNhcyI6MiwidXJpcyI6W3siZGJfbmFtZSI6ImlmcmRiMDAxIiwidGFibGUiOiJGSUxFIiwiaWQiOjQ3fSx7ImRiX25hbWUiOiJpZnJkYjAwMiIsInRhYmxlIjoiRklMRSIsImlkIjo0N31dLCJ1aWRfd2hpdGVsaXN0IjoiKiIsImlhdCI6MTU5ODE2MzMzNX0.yxQ1GrhLsWPn8Qwu42EfTDXqaYwFtrM6f_7cAH2eLRY"},"aboutme":"I am Rishav Bhowmik\r\nand this is navratna pulaow"}}`)
I get this error:
Uncaught SyntaxError: JSON.parse: bad control character in string literal at line 1 column 702 of the JSON data
the string at position 702 of the JSON string is \n
First of all, how can \n be a control character?
What should I do to resolve this?
Has this problem arrised due to MONGODB result?
\n is a control character signifying a new line. In JSON, those control characters (more specifically the \) must be escaped inside strings.
This will raise the error:
JSON.parse(`{"hello":"world\n"}`)
This wont:
JSON.parse(`{"hello":"world\\n"}`)
So one way would be to use something like replace to ensure your aboutme is properly escaped before JSON serialization. See: How to escape a JSON string containing newline characters using JavaScript?
Ok have done some experimentation and have a solution.
The Trick is to do JSON.stringify() twice,
Like,
html_text.replace('/*<whatever>*/', JSON.stringify( JSON.stringify(the_object) ) )
If suppose html_text has a line which is
<script>
const object_inbrowser = JSON.parse(/*<whatever>*/)
// no need to add qotes, `JSON.stringify` in the server will do that for you
</script>

Why does json.parse break? and how to fix it

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)

jQuery and JSON issue

I'm having an issue with the parser of jQuery when I give him a JSON string like this (java):
String JSONvalue = "[{"evaluationId":92688,"ResponseId":378501,"comment":"I can't do this ~!##$%^&*()_+|}{\":?><\/.,;'[]\\=-0987654321`","rankingId":0}]";
and when I pass it to the parser (javascript), it looks like this:
var prevCommentsAndRankings = jQuery.parseJSON('[{"evaluationId":92688,"ResponseId":378501,"comment":"I can't do this ~!##$%^&*()_+|}{\":?><\/.,;'[]\\=-0987654321`","rankingId":0}]');
I'm getting error of invalid tokens, this is because of the ' " and [ ] on the JSON string. How could I handle them, consider that the JSON may always have special characters within. Thanks in advance !
Just for the arbitrary strings replace the quote with it's HTML entity " BEFORE rendering it in JSON and right after you parsed the JSON. Both are common function you can find in Java and Javascript.

Parsing string as JSON with single quotes on key and keyvalue [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 1 year ago.
var str = "{'a':'Your's'}";
JSON.parse(str);
My server response like above string. Not able to parse as JSON. It is as example text. While am parsing I got error as below:
JSON.parse(str);
VM1951:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1(…)(anonymous function) # VM1950:1
The same question already raised by someone. click here to see. But, not helpful. Please help on this. Thanks in advance.
TL;DR JSON only supports double quotes.
Please fix the error on the server, if this is possible.
JSON required double quotes, the single quotes are therefore not conform to the standard. There may be parsers, that support them anyway, but you can never rely on that. For more details on the JSON syntax, you may have a look at http://www.json.org/.
Besides this, the input string "{'a':'Your's'}"; is totally invalid itself, if the single quotes would be valid. The ' in Your's is breaking the string literal, the following s is outside the string and the next ' is opening a string, that contains } but is never closed by another '.
The correct syntax would be '{"a":"Your\'s"}'.
If you received the string and cannot correct the server output, you may try to replace all ' by ", but you will have problems with single quotes inside your payload strings. By far the easiest - and most stable(!) - fix should be correcting the server output instead of correcting the defect output on the client.
Converting on the client with the following code may be a good idea at the first thought, but would corrupt payload strings with single quotes in it.
replaceInString = function(fullString, search, replacement) {
return fullString.split(search).join(replacement);
};
var json = replaceInString("{'a':'Your's'}", "'", '"');
If you can be sure, there are no whitespace characters outside the payload and also there are no line breaks, you could use the following function. But also only, if you are sure, the search patterns are not in the payload strings.
var json = "{'a':'Your's'}";
replaceInString = function(fullString, search, replacement) {
return fullString.split(search).join(replacement);
};
json = replaceInString(json, "{'", '{"');
json = replaceInString(json, "'}", '"}');
json = replaceInString(json, "':", '":');
json = replaceInString(json, ":'", ':"');
json = replaceInString(json, "',", '",');
json = replaceInString(json, ",'", ',"');
json = replaceInString(json, "['", '["');
json = replaceInString(json, "']", '"]');
But using this code, e.g. the JSON `following JSON string would be corrupted.
{'mathTerm':'x=1-[2+A']'}
To be clear: Code like this gets you over a bump on the road to develop , test or learn something. But this is not a durable fix. Contact the server developer to fix his implementation and remove your client side fix before going to production.
JSON keys and values must be enclosed in double quotes (") instead of single quotes (')
This is correct:
var str = '{"a":"Your\'s"}';
JSON.parse(str);

Categories