Page 1 of 1
					
				usng string function to extract values
				Posted: 28 May 2014 23:13
				by vfn
				I did this once before, but now I can't remember how I did it.... 
 
I get a string of data via http request.  It looks like this:
{"temp":79.00,"tmode":2,"fmode":0,"override":1,"hold":0,"t_cool":76.00,"tstate":2,"fstate":1,"time":{"day":2,"hour":9,"minute":35},"t_type_post":0}
(it's from the thermostats in my house)
I can get the data and save it to a file or variable no problem.
I want to extract the numeric value that follows temp and t_cool and write them to a variable to be used in the next step.
I know I can do it with "substring" but I just can't get the syntax right.
Please help.
Thanks in advance.
 
			 
			
					
				Re: usng string function to extract values
				Posted: 29 May 2014 15:30
				by Martin
				Hi,
Following script should work (please note the single quotes around the strings). This script assumes that the value is stored in variable 
s.
Code: Select all
start = indexOf(s, '"temp":')+7;
end = indexOf(s, ',', start);
v = substring(s, start, end);
The first line searches the index of the text 
"temp": and adds 7 characters to skip the 
"temp":.
The second line searches for the first comma after 
start.
The third line extracts the value 
79.00 from the original string.
This page contains some scripting examples: 
Script examples (see example 
Extract the contents of an HTML element)
You could also use the unofficial JSON to XML converter and extract the values using Xpath:
Code: Select all
xml = convertJSONtoXML('{"root":'+s+"}");
temp = evaluateXPathAsString(xml, "//temp");
t_cool = evaluateXPathAsString(xml, "//t_cool");
The first line in the script also needs to add a fake root element so that the created XML is valid.
Regards,
Martin
 
			 
			
					
				Re: usng string function to extract values
				Posted: 29 May 2014 19:51
				by vfn
				Martin,
I used your first method.  It's so simple I'm ashamed I couldn't get it right without help.
Thank you very much
			 
			
					
				Re: usng string function to extract values
				Posted: 30 May 2014 08:24
				by Martin
				No problem, glad I could help.
			 
			
					
				Re: usng string function to extract values
				Posted: 29 Sep 2019 22:46
				by Rhor
				Just wanted to jump in to thank Martin. Your solution worked. 2 days trying to find a workable script finally came to an end.
			 
			
					
				Re: usng string function to extract values
				Posted: 01 Oct 2019 17:22
				by Desmanto
				Rhor wrote: ↑29 Sep 2019 22:46
Just wanted to jump in to thank Martin. Your solution worked. 2 days trying to find a workable script finally came to an end.
 
Although you might have solved it, but the solution is from old version. Now we have fromJSON() function already, and you should use that to be more efficient.
Code: Select all
response = '{"temp":79.00,"tmode":2,"fmode":0,"override":1,"hold":0,"t_cool":76.00,"tstate":2,"fstate":1,"time":{"day":2,"hour":9,"minute":35},"t_type_post":0}';
js = fromJSON(response);
temp = js["temp"];
t_cool = js["t_cool"];
Accessing the temp and t_cool is much easier, simply point to the key of the converted json. Use Condition debug dialog after the script to check the result.