From a websocket I'm getting this data which I'm printing using console.log on a Linux console(using putty):
{"report":"IP Report:\n35.194.173.178:1"}
I want it to create a newline whereever "\n" is there in the string. How to achieve this?
What you're getting here is a JSON string. You need to parse it using JSON.parse() (inside a try/catch block because it can throw an exception on bad input - or you can use my tryjson module).
When you parse it you'll get an object whose report property is the string that you need to print.
Simplest example:
const obj = JSON.parse(yourData);
console.log(obj.report);
but make sure to add error handling:
try {
const obj = JSON.parse(yourData);
console.log(obj.report);
} catch (err) {
// handle the error
}
Related
Hey I am trying to parse a webvtt file in javascript and I wrapped the process in a try catch and it throws this error at me unsure why. Would appreciate any insight. I am using the node-webvtt package node-webvtt.
I have the full vtt file but decided to just take the first example from it and put it as a string. I have tried passing in the whole file and still get the same error. I think I might have structured the try catch incorrectly but I still got the same error not using a try catch block.
The block of code:
const parsed = (err) => {
try{
webvtt.parse(`WEBVTT
1
00:02:57,642 --> 00:02:58,672
Happy Monday, everybody.`);
}catch(err){
console.log(err);
}
}
how to handle JSON response, I got this response but I don't know how to handle it. I know variable can't started with numbers, so?
let result = [{"1d":{"volume":"22275409068573.73","price_change":"56446.71564507","price_change_pct":"0.0121","volume_change":"-13864857829188.44","volume_change_pct":"-0.3836","market_cap_change":"9216448958327.75","market_cap_change_pct":"0.0121"}}];
How to parsing"1d"?
I try JSON.parse(result[0].1d); but error happened
If you don't want to use ["prop"] to get value, you can change all props that starts with a number like in the example.
Check this link
var json = JSON.stringify(result);
json = json.replace(/,"[0-9]|{"[0-9]/g, function (x) {
return x.substring(0,1)+'"num'+x.substring(2);
});
result = JSON.parse(json);
var whatIwant = result.num1d;
You can't use JSON.parse for JSON object.
If you want to get value of 1d. Try this
result[0]["1d"]
Is it possible to implement the below way to handle the exception in expressjs Framework:
actually, I am calling a method ( Fun1 ) by a passing parameter (key_id, user_id). Like below:
data = Fun1(key_id=123, user_id=234)
so, if above line returns an error then I need to call other function (Fun2) otherwise no need to call this (Fun2)
I am implementing like this way, but it's not working( Means, when try block return error then catch block, is not executing)
try{
var data = Fun1(key_id=123, user_id=234);
}
catch(err){
var data = Fun2(key_id=123, user_id=234);
}
is there any other way to handle?
If the Fun1 throws an error then the above mentioned implantation works perfect and moves to the catch block and executes the Fun2 but if it returns a value then this does not work
Syntax for throwing an error
throw new Error("<Error Message>")
async changeCarOwner(ctx, carNumber, newOwner) {
const carAsBytes = await ctx.stub.getState(carNumber);
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
car.owner = newOwner
await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
}
}
I keep getting an error: Unexpected end of JSON input. Why? I am trying to update an existing key-value pair in couchDb using the above code.
This error happens at this line:
const car = JSON.parse(carAsBytes.toString());
It is due to the fact that carAsBytes.toString() does not evaluates to a properly formatted JSON string. The code you show seems fine, but the error is coming from elsewhere in your code.
Debugging tip: use the debugger statement to examine variables before the faulty line, simply add a console.log(carAsBytes.toString()) before it.
I am working with node-red and I would like to create my custom function to access some index from the incoming message. The incoming message looks like this
{ "node": "02010101", "base64": "Cro=" }
It comes out from the json function block in node-red (A function that parses the msg.payload to convert a JSON string to/from a javascript object. Places the result back into the payload), I can use the debug block to obtain the index base64, however if I try to do the same with my own function to proces that later, I cannot and I get the error
TypeError: Cannot assign to read only property '_msgid' of Cro=
My function is really silly for now it is just
return msg["base64"];
I understand that it complains that there is no property in the incoming message, so I would like to access to hat index, how can I do it?
EDIT: if I set the debug block to show the whole message object not just the msg.base64 itself, I get this
{ "node": "02010101", "base64": "Cro=", "_msgid": "6babd6e.f945428" }
A function node should return a whole msg object not just a string.
If you want to just send on the string value you should do something like this:
msg.payload = msg.payload["base64"];
return msg
THe solution was easy, just return the whole message and not just a field. Using the following snippet made it work.
Getting decrypted module
msg.payload = msg.decrypted;
return msg;
base64Decode
msg.payload = msg.base64;
return msg;