Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse json in javascript
#1
Hi, I want to parse the following json string:

{"Time":"2023-09-06T13:38:56","BME280-76":{"Temperature":28.7,"Humidity":47.9,"DewPoint":16.6,"Pressure":1016.7},"BME280-77":{"Temperature":28.6,"Humidity":92.6,"DewPoint":27.3,"Pressure":1017.0},"PressureUnit":"hPa","TempUnit":"C"}


The first javascript line gives already an error


Code:
var json = Virtuino.readValue("inputTag"); //Read inputTag value as String. 
    
ERROR: Unexpected identifier 'Time'. Expected ';' after variable declaration.

If I run this:
Code:
const obj = JSON.parse(Virtuino.readValue("inputTag")); //Read inputTag value as String.   

the error is:
ERROR: Unexpected identifier 'Time'. Expected ')' to end an argument list.

I know I can use the 'Jason to Variable' script tool, but that would require more then 10 scripts to get all the variables.

Any idea?

THX Joe
Reply
#2
Hi,
is there really no way to get a Json string into javascript?

This is in V0 inputTag:
Code:
{"Time":"2023-09-09T19:05:35"}

this javascript does't work:

Code:
//var inputValue='{"Time":"2023-09-09T19:05:35";' // this is working
var inputValue = Virtuino.readValue("inputTag");  // does't work

if (inputValue!=null)  {       
  const obj=JSON.parse(inputValue);              
  result = '{"tag":"outputTag", "value":"'+ obj.Time+ '"}';   
}
else  {    
  result = null;
}

 Need something like "Virtuino.readJson"
          
Best regards, Joe
Reply
#3
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];


Attached Files Thumbnail(s)
   
Reply
#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


Forum Jump:


Users browsing this thread: 1 Guest(s)