I am using the WeMo node plugin for Node Red with a WeMo switch.
Using the "WeMo Lookup" function I receive the following message payload:
2/8/2018, 12:30:34 PMnode: 6dece90e.84b898 WemoCheck : msg.payload :
Object { state: 1 }
From what I understand, I should use the JSON Function to obtain the actual value. Using the JSON function with the format option enabled I receive the following message payload:
2/8/2018, 12:41:11 PMnode: 5ae2b7bf.1e0e98 WemoCheck : msg.payload :
string[18] "{↵ "state": 1↵}"
I believe this is the desired result.
However I cannot obtain just the value using a function. I have tried the following two and both return "msg.payload : undefined"
var value = msg.payload[0].state;
msg.payload = value;
return msg;
or
var value = msg.payload.state;
msg.payload = value;
return msg;
I think I might be missing something obvious. Any assistance would be appreciated.
You don't need to use the JSON node at all, the output is already a JSON object.
If you just want the payload to be 1 or 0 from the state value then your second version of your function node should work (even if it has an extra un needed step).
msg.payload = msg.payload.state;
return msg;
You shouldn't need a function node to do this, the change node will let you move values around with something like this
Related
I am trying to pull out the value "533"
{
"d": {
"ItemValue_1": 533
},
"ts": "2021-01-20T10:59:41.958591"
}
This does not work
var ItemValue_1 = msg.payload.ItemValue_1;
msg.payload = ItemValue_1;
return msg;
My result is unsuccessful
I was able to solve on my own, it works.
sensorMeasurement=JSON.parse(msg.payload);
msg.payload=sensorMeasurement.d;
var msgout = {payload : msg.payload.ItemValue_1}
return msgout;
The better way to do this is as follows:
Add a JSON node before the function node, this will turn a string payload in to a JSON object (assuming the string actually represents a JSON object).
Then if you are using a function node the following:
msg.payload = msg.payload.d.ItemValue_1;
return msg;
It is bad practice to create a new object as you did in your answer as it throws away any meta data that may be required later that is attached to the original object.
Rather than use a function node it would also be cleaner to use a change node and the Move mode to shift the msg.payload.d.ItemValue_1 to msg.payload
I an using JSON and NODEJS
i am receiving data from TCP and i was receiving the data like this
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]
or
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"some n letters]
like this it is truncating while receiving TCP data i want to remove that half received data or is there any way to receive the full buffer
I want the Output to be
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" }]
A clumsy, but simple way would be to take the string/buffer and try to parse using JSON.parse(). Wrap it in a try/catch and only set the final value when it succeeds.
var badJson = '[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]';
try {
var result = JSON.parse(badJson);
}
catch(e) {
console.log('bad json, not parsing yet');
}
I am doing an Ionic App with typescript.
I have some error condition as response from REST API,
I did
err._body
and it gives me
{"reason":"invalid_token"}
but when I do
err._body.reason
or
err._body.get("reason")
it gives undefined value.
I did JSON stringify and parse as well, no luck,
How to parse this and get the value so that I can apply specific processing for this.
First try to do
console.log(typeof err._body);
so we can be sure what the type of that is. If it's a string, you should do
let errorObj = JSON.parse(err._body);
// And then...
let errorMsg = errorObj.reason // or errorObj["reason"] as well
If it's an Object, you can skip the parse() part and just use it like this:
let errorMsg = err._body.reason // or err._body["reason"]
Hi I'm new to Jquery and i don't know how to use json data. I have a Json which is coming as a response. My success function is
success: function(resp)
{
var x = JSON.stringify(resp);
alert(x);
}
alert is having following JSON
{"xyz":{"abc":[{"action":"fail","result":"xxx"},{"action":"pass","resut":"yyy"}]}}
I want action value alone. How can i get this value from x variable and how can i utilize this value in HTML. Thanks in advance.
When you use JSON.stringify() you are turning it into a string, you want it as an object to access the data. Try this:
success: function(resp) {
alert(resp.xyz.abc[0].action);
var x = resp.xyz.abc[0].action // to use the value in html later on
}
If it is returned as a string (I can't tell at this point), you can turn it into an object (as long as it is valid JSON) by using $.parseJSON()
success: function(resp)
{
var x = $.parseJSON(resp);
var xyz = x.xyz;
var pass = x.xyz.abc[1].action;
}
or you can loop though each of the array by $.each
success: function(resp)
{
var x = $.parseJSON(resp);
$.each(x.xyz.abc, function(index, element){
alert('action:' + element.action + ', result:' + element.resut)
});
}
i think, and don't take it personally,that your JSON object is not well organized as an object to get and to have. Action,from my perspective is either fail or success, and reading it, as you saw in the above good answer, will give you exactly what you want.
What's the point in getting a JSON with data structured like you did, with 2 possible answers encoded in it, when there is only one available (either success or fail).
I'm trying to make a small parser which receives the json string and the path to get:
var args = process.argv;
var jsonToBeParsed = args[2];
var path = args[3];
var result = JSON.parse(jsonToBeParsed);
console.log(result);
console.log(result.path);
I'm calling this with
node parser.js '{"asd":"123", "qwe":"312"}' 'asd'
It gives me undefined
I think it has to be done with some eval function but I don't have too much experience with node/JS.
How can I resolve this?, I need to get the result from the command line.
Edit: I'm expecting "123" in the second log. Thanks #Esailija, the question wasn't too clear ...
I think you are trying to use dynamic property, you cannot use .path, because that literally means .path property.
Try this:
console.log(result);
console.log(result[path]);
if path === "asd", then it will work, which is statically equivalent to result["asd"] or result.asd
To add to #Esailija's answer:
node parser.js '{"path": "123"}' 'asd'
Would return 123. The dot notation expects the property name. If you have a dynamic property name, you need to use the square brackets notation.
console.log(result['ads']);
// But what you want is:
console.log(result[path]); // 'path' is a variable with the correct string
I wanted to embed the json parser into a git alias, To get a json node in bash (using node)
node -e "console.log(JSON.parse(process.argv[1]).foo.bar)" '{"foo":{"bar":"Hello World"}}'