Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse json in javascript
#4
(09-12-2023, 09:41 PM)iliaslamprou Wrote: Hi Joe,

It took me quite some time to figure out how to resolve this issue.
From the next update the function Virtuino.readValue  will be able to read the Json message


This is an example:
Code:
/*
This script reads values from a Json
e.g. {"temperature":34.5, "room":"bedroom","state":true}
*/

//--- Copy values from Virtuino Variables to JavaScript variables
let inputValue = Virtuino.readValue("inputTag");   // read the json text

//--- enter the code here
const obj = JSON.parse(inputValue);  
var value = obj.temperature;
var room = obj.room;
var state = obj.state;
result1 = '{"tag":"outputTag1", "value":'+value+'}';     
result2 = '{"tag":"outputTag2", "value":"'+room+'"}'; 
result3 = '{"tag":"outputTag3", "value":"'+state+'"}'; 
result=[result1,result2,result3];

Ilias, you are my hero!

Sorry that this has stolen so much of you free time.

All the best, Joe
Reply


Messages In This Thread
Parse json in javascript - by Joe - 09-06-2023, 12:43 PM
RE: Parse json in javascript - by Joe - 09-12-2023, 07:37 AM
RE: Parse json in javascript - by iliaslamprou - 09-12-2023, 09:41 PM
RE: Parse json in javascript - by Joe - 09-13-2023, 07:04 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)