Extract value from json

Hi guys,
I searched in the various old posts but there is nothing that is simple enough for me to be able to integrate what I will try to summarize as briefly as possible.

The goal is to bring two values ​​of my central heating unit into a two different Sensor Widged or into a new Custum widget.
These values ​​are the Internal Temperature and the Set Temperature.

Since the heating system is connected online, after much research I managed to find the http string to get these values.
Through the Html command, I receive a JSON response from the broswer in which are listed, (among other Data), the internal temperature and the set temperature.

Please, is there anyone who is able to extract data from the Json response, so that it can be displayed into a widget within HomeGenie?

Below is a part of the Json response that contains the data set for the internal temperature (roomTemp) and set temperature (ConfortTemp Value).
{ “Mode”: 2, “allowedModes”: [0,1,2,5], “outsideTemp” 7.0, “holidayEnabled”: false, “holidayUntil”: “0001-01-01T00: 00: 00”, " dhwTemp “: {” min “: 35.0,” max “: 65.0,” value “: 35.0,” prevValue “0.0,” step “: 0.0},” flameSensor “: false,” dhwDisabled “: false,” zones " { “mode”: { “allowedOptions”: [2.3], “value”: 2}, “comfortTemp”: { “min”: 10.0, “max”: 30.0, “value”: 24.0, “prevValue” : 0.0, “step”: 0.0}, “desiredTemp” 0.0, “roomTemp”: 21.9, “derogaTemp”

Have you looked at modifying the Weather program. That might be suitable for your project.

Hi Peter,
exactly… I looked at the weather program and other old posts that talk about similar situations. But the indications are too general to succeed with my knowledge.
Maybe you are able to do this?

It was the first program that came to mind that manipulated JSON strings. Have you considering deploying MQTT and publishing the results to HG. Again this is only a suggestion on first read. Maybe other users might have a more practical suggestion.

I was able to parse a similar JSON string like this:

   var poolcontrollerData = Net.WebService(webserviceurl).GetData();
   string currentwatts = poolcontrollerData["pump"]["1"]["watts"];

webserviceurl is a string variable containing the REST URI e.g. http://hostname:port/path

This was the only method I found that would work with my JSON string because the node names are numeric.

Thank you very much for your seggestion!
I just sat in front of the PC, Jay, I’ll try your suggestion immediately and let you know my results.


Unfortunately I’m continuing to make attempts, because for the moment I get the following error:

"The best overloaded method match for` string.this [int] 'has some invalid arguments "

I’m trying to figure out if the reason for this error is due to the incorrect insertion of the Json data that I have to query. (I am currently trying the roomTemp value)

I will update you as soon as (hopefully), I will be able to use Joe’s String.

C# programs reference Newtonsoft.Json package, so you can do something like this:

var s = "{\"Mode\":2,\"allowedModes\":[0,1,2,5],\"outsideTemp\":7.0,\"holidayEnabled\":false,\"holidayUntil\":\"0001-01-01T00: 00: 00\",\"dhwTemp\":{\"min\":35.0,\"max\":65.0,\"value\":35.0,\"prevValue\":0.0,\"step\":0.0},\"flameSensor\":false,\"dhwDisabled\":false,\"zones\":{\"mode\":{\"allowedOptions\":[2.3],\"value\":2},\"comfortTemp\":{\"min\":10.0,\"max\":30.0,\"value\":24.0,\"prevValue\":0.0,\"step\":0.0},\"desiredTemp\":0.0,\"roomTemp\":21.9}}";

dynamic d = JObject.Parse(s);
Program.Notify("Test3", d.zones.comfortTemp.value.ToString());

Here in s you should place response from your heating unit server and adjust d.zones.comfortTemp.value to reflect actual response from server.

That’s exactly what we’ve been missing here. Straight to the point solutions.

Grazie Bounz!
Happy New Year to all of you!!

Sorry, I haven’t been able to check your answers before …
I tried the script and it works, but I’m already stuck again!
The first problem is that in the server’s original json response there are no " \ ", and without those I get an error (I think it is due to the fact that there are others “” inside the answer, which suggest that the variable is finished.
Also, who would be able to kindly tell me what is the best way to assign the answer you receive from a web link to the variable? (Maybe the response should be scheduled to be saved at set intervals in a txt file, so that the content of the saved file can be assigned to the variable?)
Please, does anyone have any examples done in this regard?

Roberto, happy New Year to you, too!

In my example backslashes \ have been added by me, because I included sample json string in code. In real-world scenario when you communicate with external server and process its response this should be no issue.

Even more, when looking for example I found this in documentation: https://genielabs.github.io/HomeGenie/api/ape/class_home_genie_1_1_automation_1_1_scripting_1_1_net_helper.html#a08d047072bdd8bcefd1645de3dc6eda0
NetHelper class has GetData() method which parses JSON response from server.

Here is a little test program that asks data from remote server, stores it in a variable, and then displays it via notification:

var dateResponse = Net.WebService("http://date.jsontest.com").GetData(); 
Program.Notify("Response", dateResponse.ToString());
Program.Notify("Parsed", dateResponse.date.ToString());

Response from the server is in the following format:

   "time": "03:53:25 AM",
   "milliseconds_since_epoch": 1362196405309,
   "date": "03-02-2013"

Bounz, you are number One as always !!!
Your script works perfectly and it is very simple!!!
I’m working on it from a few hours …(because I’m looking for the solution to login to the link!!)
As soon as possible I will inform you all about the success… or about launching the PC from the window!! :firecracker: :firecracker: :computer: :firecracker: :firecracker: :grin: :grin: :grin:

To summarize a long investigation of Roberto’s case: the problem was in authentication at remote server. There was no API and the only method to authorize data request was to make autorization request, grab authorization cookie and use with data request.

And because HG program helper doesn’t provide System.Net.Http namespace to custom programs I had to use old obsolete classes like WebClient and WebRequest :cry:

The final solution:

Uri ServiceUri = new Uri("https://service.tld");
string LoginUrl = "https://service.tld/Account/Login";
string DataUrl = "https://service.tld/path/to/data/request";
string Email = "[email protected]";
string Password = "password";

  using (WebClient wc = new WebClient())
    var reqParams = "Email=" + Uri.EscapeDataString(Email) + "&Password=" + Uri.EscapeDataString(Password);
    var data = Encoding.UTF8.GetBytes(reqParams);

    var req = WebRequest.CreateHttp(LoginUrl);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.AllowAutoRedirect = false;
    req.ContentLength = data.Length;
    using (var dataStream = req.GetRequestStream())
      dataStream.Write(data, 0, data.Length);

    var resp = (HttpWebResponse)req.GetResponse();
    var cc = new CookieContainer();
    cc.SetCookies(ServiceUri, resp.Headers["Set-Cookie"]);

    wc.Headers.Add(HttpRequestHeader.Cookie, cc.GetCookieHeader(ServiceUri));
    var jsonString = wc.DownloadString(DataUrl);
    dynamic d = JObject.Parse(jsonString);
    Program.Notify("Test3", d.zone.comfortTemp.value.ToString());
catch (Exception e)
  Program.Notify("ERR", e.Message + " " + e.StackTrace);