JSON.parse without escaping [duplicate] - javascript

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 6 years ago.
Is there anyway to do this in JavaScript:
$ cat test.json
{"body":"\u0000"}
$ python3 -c 'import json; print(json.load(open("test.json", "r")))'
{'body': '\x00'}
Notice, the data above only one \ (does not need to be escaped). So you have the following situation in JavaScript:
JSON.parse('{"body":"\\u0000"}') // works
JSON.parse('{"body":"\u0000"}') // does not work
With potentially any UTF-8 data comming from a binary source (websocket), can this data be processed directly like in the first python example above?

String characters from \u0000 through \u001F are considered as control characters, and according to RFC-7159 are not allowed characters to use in JSON and must be escaped, as stated in section 7.
What you are trying to do is to put unescaped control characters into a JSON, which clearly not acceptable, you have to escape it first, non of the languages accept it, even Python.
The correct answer would be place a UTF-8 encoded value into a string containing a JSON format.
This is a correct JSON, and will be parsed by any JSON parser in any language, even in JavaScript:
{"body":"\u0000"}
This is incorrect JSON (consider the [NUL] as a NUL control character, as it cannot be represented in text):
{"body":"[NUL]"}
That's why JSON.parse('{"body":"\\u0000"}') works and JSON.parse('{"body":"\u0000"}') doesn't.
Hope, it clarifies what's wrong with your test.

Related

Getting string from another string in Javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "

UTF-8 to readable characters [duplicate]

This question already has answers here:
Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings
(12 answers)
Closed 4 years ago.
Encoding and decoding a string is not as easy as I thought.
The original string is as follows:
at the end of → al término de • después de
After PHP base64 encoding (used three times) it looks different:
VUVkSksxbFlVV2RrUjJoc1NVZFdkVnBEUW5aYWFuZDJXV28wWnpSdllWTkpSMFp6U1VoVVJIRllTblJoVnpWMlNVZFNiRWxQUzBGdmFVSnJXbGhPZDJSalQzQmplVUpyV2xSNGQxQm5QVDA9
When trying JS window.atob() to decode the string, the result is this:
at the end of â al término de ⢠después de
UTF-8 characters are not displayed properly. What function should I use to fix this?
Try
let decodedString = decodeURIComponent(escape(window.atob(yourString)))
What happens with this is that JavaScript does not allow characters that are outside the range of Latin1, to be able to execute it you could do the following:
let str = 'at the end of → at the end of • after';
btoa(unescape(encodeURIComponent(str)))
// output: YXQgdGhlIGVuZCBvZiDihpIgYXQgdGhlIGVuZCBvZiDigKIgYWZ0ZXI=
This code will convert (from javascript) the string correctly to base64 and from PHP you can decode it well, now the problem is that JavaScript will not be able to decode it since it interprets it as the syntax is wrong (although it has been encoded) and so It will not work for you either.

How to escape double and single quote from JS string [duplicate]

This question already has answers here:
Javascript, Razor and Escape characters. Like apostrophe
(5 answers)
Closed 7 years ago.
I'm assigning the value of JS string variable from server side code.
Like this
var fbShareTitle = "#(ViewBag.LeadTitle as string)";
ViewBag return string value is
A "Fantastic" Lead With 'Qoute'
Now It is giving error in console
SyntaxError: missing ; before statement
I have tried this
var fbShareTitle = ("#(ViewBag.LeadTitle)").replace(/"/g, '\\"');
But now I'm getting this error.
SyntaxError: missing ; before statement
As This string will be shared on fb, So i can't modify string, like replace all " with ' e.t.c.
The reason why your code doesn't work is that Razor will generate the following:
var fbShareTitle = "A "Fantastic" Lead With 'Qoute'";
which is invalid JavaScript. You can't simply fix it by replace, since it's not the problem that your string is bad, it's that your code can't parse - replace never gets to execute. You need to fix it on serverside, where you generate the JavaScript in question, by modifying your Razor code:
var fbShareTitle = #Html.Raw(Json.Encode(ViewBag.LeadTitle as string));
Json will take care of quotes and proper escaping; Raw will make sure you don't get your < and > replaced. Extra benefit from #Html.Raw(Json.Encode(...)) mantra: you can use it to inject any kind of data that can be encoded in JSON, not only strings.

Javascript / jQuery: How to convert string to HTML character code [duplicate]

This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show &#39 instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "&#39") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}

Can single quotes and double quotes be used interchangeably while enclosing strings in javascript? [duplicate]

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
Till now whatever programming languages I have encountered use double quotes for string and single quotes for characters but some of the javascript code that I saw seemed to be using them interchangeably,is it possible?
In which situations can they be interchanged?
In which situations they CANNOT be interchanged?
They are interchangeable, but when you open a string with one, you must close it with the same character. For instance,
"Hello' would not be a valid string.
EDIT: PS, this allows for you to enclose quotes in a string. For example, if you want to have the string literal "Hello", you could have
var string = '"Hello"'
yes they can as long as you keep in mind a few main things;
1- double quotes are the preferred by the browser to se they parameters values, so you never use them when setting javascript values or function calls within the DOM.
2- double quotes are also used to specify json an properties a json string using single quotes to delimiter they key, value pairs is not valid.
3-they don't complement each other, so what one starts one must end.
4 - if you need to use the same quote inside your string you need to scape it.

Categories