Javascript output json string how to do .replace - javascript

I have this script in javascript.
socket.onmessage = function (event) {
var theDiv = document.getElementById("cli");
var JSONObject = JSON.parse(event.data);
if(JSONObject.event === "console output") {
theDiv.innerHTML += "<div>" + JSONObject['args'] + "</div>";
}
};
the outcome of JSONObject['args'] is this:
>[2K [16:10:47] [Server thread/INFO]: [Server] test
The string is this
{"event":"console output","args":["\u003e\u001b[2K\r[16:10:47] [Server thread/INFO]: [Server] test"]}
How can i remove this from the string \u003e\u001b[2K\r?
i tried this but it doesn't work
socket.onmessage = function (event) {
var theDiv = document.getElementById("cli");
var JSONObject = JSON.parse(event.data.replaceAll("\u003e\u001b[2K\r", ""));
if(JSONObject.event === "console output") {
theDiv.innerHTML += "<div>" + JSONObject['args'] + "</div>";
}
};
How can I make it work? like this
{"event":"console output","args":["[16:10:47] [Server thread/INFO]: [Server] test"]}

Do the replacement after parsing the JSON.
You need to use map() because JSONObject.args is an array.
theDiv.innerHTML += "<div>" + JSONObject.args.map(arg => arg.replaceAll( "\u003e\u001b[2K\r", "")) + "</div>";
Otherwise you need to escape all the backslashes, so they'll be treated literally.
var JSONObject = JSON.parse(event.data.replaceAll("\\u003e\\u001b[2K\\r", ""));
But it's generally a bad idea to manipulate the JSON string directly, as you may invalidate the JSON. It's usually best to parse the JSON and then deal with the resulting object/array.

You can try to convert toString() and then remove the value
JSONObject['args'].toString().replace('\u003e\u001b[2K\r', '')

Related

JSON parser keeps failing on newline even after adding another backslash

So I'm making a messaging platform for a school project, and I'm trying to both protect my database, protect XSS attacks, and also allow every single character, including newlines.
So far, I've got everything covered, except newlines. JSON parser cannot for the live of it parse a simple newline.
I've tried prefixing the newline with another backslash (\\n) but that STILL doesn't work!
What should I do?!
Edit 1: Added code that generates the JSON data
string message = "[";
string uid;
for (int i = 0; i < data.Rows.Count; i++) {
if (StringCipher.ConvertToUnixTimestamp(DateTime.Now.AddMinutes(-5)) > int.Parse(data.Rows[i][5].ToString())) {
queryDelete = "DELETE * FROM Bubbleland WHERE MessageID = " + data.Rows[i][0] + ";";
commandDelete = new OleDbCommand(queryDelete, connection);
commandDelete.ExecuteNonQuery();
} else {
message += "{\"mid\":" + data.Rows[i][0];
if (data.Rows[i][1].ToString() == "")
uid = "user";
else
uid = data.Rows[i][1].ToString();
message += ", \"uid\":\"" + uid;
message += "\", \"name\": \"" + data.Rows[i][2];
message += "\", \"color\": \"" + data.Rows[i][3];
message += "\", \"content\": \"" + data.Rows[i][4];
message += "\"}";
if (i + 1 < data.Rows.Count) {
message += ",";
}
}
}
message += "]";
Edit 2: Added JS processing code
async function fetchMessages() {
$.ajax({
type: "POST",
url: "../ASPX/bubblelandFetch.aspx",
success: function (data) {
console.log(data);
data = JSON.parse(data);
var container = document.getElementById("messages-container");
var uid;
container.innerHTML = "";
console.log(data);
for (var i in data) {
var message = '<div id="' + data[i]["mid"];
message += '" class="message ' + data[i]["color"] + '">';
message += '<div class="profile-container">';
if (data[i]["uid"] == "user")
uid = "/Media/user"
else
uid = "/Media/Profile/" + data[i]["uid"];
message += '<img src="' + uid + '.png"/>';
message += '<span>' + data[i]["name"] + '</span></div>';
message += '<div class="content">' + data[i]["content"] + '</div>';
container.innerHTML += message;
}
},
complete: function () {
setTimeout(fetchMessages, intevral);
}
});
}
After messing around with parsing the string you provided me, I have stumbled across a fix and did further research to understand why this happened in the first place.
To put it simply:
‘Single quotes “escape” single quotes’
“Double quotes ‘escape’ double quotes“
These quotes mean, when using single quotes to create a string literal, you must escape any single quotes with a backslash \ for the string literal to be valid. The same goes for when using double quotes to create a string literal, but escaping double quotes with a backslash.
I didn't recognize this issue at first, even though I knew the problem in the first place and I do this all the time.
I came across this Medium article helps put things together, and I suggest you read it to freshen up on using single/double quotes with string literals.
As it turns out, an unescaped \r was messing up the JSON parser.
Whenever you Shift + Enter, it puts \r\n instead of a simple \n. I had no idea it does this. So in my ASPX backend code, I replace \r\n with <br>.

JSON Parse causing 'undefined' json values - Javascript

I have searched far and wide for answers to my problem but I am just not winning, I am hoping someone will be kind enough to offer me some guidance.
My below Javascript code is returning undefined json values:
var req = '{"testId":"12345","ruleId":"678910","rulePassed":true,"testValue":"C:\\ProgramTest\\"}'
var stringified = JSON.stringify(req);
console.log('stringified json ' + stringified);
//json = JSON.parse(JSON.stringify(stringified))
var json = JSON.parse(stringified );
console.log('parsed json ' + json);
//testing different ways of pulling out the data, all undefined
var testId = json["testId"];
var ruleId = json.ruleId;
var testValue = json[testValue];
console.log('testValue ' + testValue);
var rulePassed = Boolean(json[rulePassed]);
njson = '{"testId": "' + testId + '","ruleId": "' + ruleId + '","testValue": "' + testValue + '","rulePassed": ' + rulePassed + '}';
console.log('final json ' + njson);
The complication comes in with the backslash in the testValue property.
If I do not stringify the json first, I receive the following error:
SyntaxError: Unexpected token P in JSON at position 143
As soon as I Stringify however, and then parse, the values come back as undefined.
Does anybody perhaps know what I am doing wrong please?
Thanks
If you know that your data will never properly escape backslashes, a quick solution is the following:
var req_escaped = req.replace(/\\/g, "\\\\") // escape backslashes
JSON.parse(req_escaped)
Basically, make your string JSON compliant and then use the usual parsed method.
replacing the backslashes compiles. also you need to add " around testValue when you get it from the json
var req = '{"testId":"12345","ruleId":"678910","rulePassed":true,"testValue":"C:\\ProgramTest\\"}';
var req_escaped = req.replace(/\\/g, "\\\\") // escape backslashes
var json = JSON.parse(req_escaped);
console.log(json);
var testId = json["testId"];
var ruleId = json.ruleId;
var testValue = json["testValue"];
console.log('testValue ' + testValue);
var rulePassed = Boolean(json[rulePassed]);
njson = '{"testId": "' + testId + '","ruleId": "' + ruleId + '","testValue": "' + testValue + '","rulePassed": ' + rulePassed + '}';
console.log('final json ' + njson);
If you have control over the escaping process, the backslashes path should be escaped as follows:
\\ should be \\\\
The first escape escapes it in the Javascript string literal. The second escape escapes it in the JSON string literal. Credits and more details.
var req = '{"testId":"12345","ruleId":"678910","rulePassed":true,"testValue":"C:\\\\ProgramTest\\\\"}'
console.log(JSON.parse(req))

How extract some part of a javascript from a webpage using jsoup?

i want to extract some data from blow script
$(document).ready(function(){
$("#areaName").val(1);$("#state").val(29);$("#city").val(1);
$("#subareaName").val(1);$("#lane").val(1);
}
like areaName value = 1, state value = 29, city value = 1, subareaName value = 1, lane value = 1
How can i achieve this using jsoup?
Jsoup is html (xml) parser. You can use it to extract javascript from page source for example like this: Elements scripts = document.select("script");
Then you will have to parse the script by yourself. You can use regex to do so.
Here is an example.
final String propertyName = "areaName";
final String regex = "#" + propertyName + ".*?val\\((.*?)\\)";
final String script = "$(document).ready(function(){ \n"
+ " $(\"#areaName\").val(1);$(\"#state\").val(29);$(\"#city\").val(1);\n"
+ " $(\"#subareaName\").val(1);$(\"#lane\").val(1);\n"
+ "}";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(script);
if (matcher.find() && matcher.groupCount() > 0) {
String areaName = matcher.group(1);
System.out.println(propertyName + ": " + areaName);
}

Using variables in Javascript.replace

I am trying to inject the replace method for javascript in webview for android.
This code works:
{
mWebView.loadUrl("javascript:(function(){" +
"document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');" +
"})()");
}
Instead of putting the string in the method, however, I want to use variables. I tried using regex but it does not seem to work.
{
String old = "hello";
String new = "hi";
mWebView.loadUrl("javascript:(function(){" +
"var ol = new RegExp(old,'g');" +
"document.body.innerHTML = document.body.innerHTML.replace(ol, new);" +
"})()");
}
Is there something off with my code?
You have to quote out the variables when passing them into a string like that
{
String old = "hello";
String _new = "hi";
mWebView.loadUrl("javascript:(function(){" +
"var ol = new RegExp("+old+",'g');" +
"document.body.innerHTML = " +
"document.body.innerHTML.replace(ol, " +_new+ ");" +
"})()"
);
}
Note that new is a reserved keyword, and shouldn't be used as a variable name

The regex test in jQuery's parseJSON returns false - why?

I don't understand why the test in jQuery's parseJSON function:
/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "#")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))
returns false for the string:
{"TermTitle":"some Title"}
The test in http://www.jsonlint.com/ tells me that the string {"TermTitle":"some Title"} is valid JSON, but when I try to pass it into $.parseJSON(opData) the parseJSON function fails...
I also tested just this /^[\],:{}\s]*$/.test... function separately in Firebug with the mentioned string.
[edit]
Ok, code:
var json = '{"TermTitle":"some Title"}';
var obj = $.parseJSON(json);
alert('obj = ' + obj + ' and obj.TermTitle = ' + obj.TermTitle);
works also for me.
But in my case where I have the following in my JavaScript:
function GetTerm($iInd) {
$.ajax({
type: "POST",
url: "get_term.php",
data: {
id: $iInd
},
success: function(data){
ProcessFetchedTerm(data);
}
});
//If I tried adding dataType: "json" then it would stop working at all.
}
function ProcessFetchedTerm(opData) {
alert ("In ProcessFetchedTerm, data: '" + opData + "'");
alert ("typeof opData: " + typeof opData);
alert ("Replacement result of the regex function: " +
opData.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "#").
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").
replace(/(?:^|:|,)(?:\s*\[)+/g, ''));
alert ("Result of the regex function: " +
/^[\],:{}\s]*$/.
test(opData.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "#").
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").
replace(/(?:^|:|,)(?:\s*\[)+/g, '')));
var oTerm = $.parseJSON(opData);
alert('oTerm = ' + oTerm + ' and oTerm.TermTitle = ' + oTerm.TermTitle);
}
and in get_term.php I have:
echo json_encode(
array(
"TermTitle"=>"some Title"
)
);
The alerts return:
In ProcessFetchedTerm, data: '{"TermTitle":"some Title"}'
typeof opData: string
Replacement result of the regex function: {]:]}
Result of the regex function: false
The last alert is not shown
[edit 2]
I rewrote the beginning of my function ProcessFetchedTerm to
function ProcessFetchedTerm(opData) {
var json = '{"TermTitle":"some Title"}';
alert ("In ProcessFetchedTerm, opData: '" + opData + "' json: '" + json + "'");
var oTerm = $.parseJSON(json);
the alert puts out:
In ProcessFetchedTerm, opData: '{"TermTitle":"some Title"}' json: '{"TermTitle":"some Title"}'
So the strings are equal and in case the next line is var oTerm = $.parseJSON(json); it works but if next line is var oTerm = $.parseJSON(opData); it does not work.
So what could be hidden inside this (presumably) string object opData that prevents it from working?
Running this code:
var json = '{"TermTitle":"some Title"}';
var obj = $.parseJSON(json);
alert('obj = ' + obj + ' and obj.TermTitle = ' + obj.TermTitle);
works for me (with jQuery 1.4). The problem must be elsewhere in your code. What is the exact code you are running when the parseJSON method fails?
Edit (response to posted code):
Why are you doing the functionality of your 3rd and 4th alert? I would guess that that is changing your opData value. Try adding your first alert right before your parseJSON call and see what it is there.
It returns "false" because it doesn't match. The "replace" calls transform that JSON string into `{]:]}', which does not match the regex.
[edit] oh durrr yes it does match; well I don't know what your problem is. Why do you think it returns "false"?

Categories