Announcement

Collapse
No announcement yet.

Looking for a sample JSON parser script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for a sample JSON parser script

    Anyone have a handy sample JSON parsing script they can share? I have a remote device that has a built in web server that outputs JSON, and I was looking to parse it's output to update an HS device.

    Code:
    {"data":{ "id":"110000AE","type":"1","detector":"SBM20","cpm":30,"temperature":25.25,"uptime": 309313}}
    Thanks!

  • #2
    I don't really know what I am doing but this should work, you will need to reference the System.Web.Extensions.dll in your settings.ini for it to work.

    Code:
    Imports System.Web.Script.Serialization
    
    Sub Main(ByVal Parm As Object)
    
        Try
            '{"data":{ "id":"110000AE","type":"1","detector":"SBM20","cpm":30,"temperature":25.25,"uptime": 309313}}
            Dim InputString As String = "{""data"":{ ""id"":""110000AE"",""type"":""1"",""detector"":""SBM20"",""cpm"":30,""temperature"":25.25,""uptime"": 309313}}"
            Dim json As New JavaScriptSerializer
            Dim dataObj As jsonStructure = json.Deserialize(Of jsonStructure)(InputString)
    
            hs.writelog("id", dataObj.data.id)
            hs.writelog("type", dataObj.data.type)
            hs.writelog("detector", dataObj.data.detector)
            hs.writelog("cpm", dataObj.data.cpm)
            hs.writelog("temperature", dataObj.data.temperature)
            hs.writelog("uptime", dataObj.data.uptime)
    
        Catch ex As Exception : hs.writelog("Test Script", "Error:  " & ex.Message.ToString)
        End Try
    
    End Sub
    
    Public Class jsonStructure
        Public data As data
    End Class
    
    Public Class data
        Public Property id As String
        Public Property type As String
        Public Property detector As String
        Public Property cpm As Integer
        Public Property temperature As Decimal
        Public Property uptime As Double
    End Class
    My Plugins:

    Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
    Yeelight 3P | Nanoleaf 3P

    Comment


    • #3
      I also use the Newtonsoft JSON libraries to make life easier. That will parse a JSON string to a class for you with ease.

      As mrH says above, you'll need to add the reference to your in INI file to use it in scripts.


      Sent from my iPad using Tapatalk HD
      Author of Highpeak Plugins | SMS-Gateway Plugin | Blue Iris Plugin | Paradox (Beta) Plugin | Modbus Plugin | Yamaha Plugin

      Comment


      • #4
        Thank you guys! Worked like a champ I had an issue getting the settings.ini file going for the reference... but finally figured it out. Really appreciate it.

        EDIT: For those curious, all I had to do was add the following to the settings.ini:
        Code:
        ScriptingReferences=System.Web.Script.Serialization;System.Web.Extensions.dll

        Comment


        • #5
          Also this is what its for; http://www.uradmonitor.com/

          I have one of the radiation sensors and wanted to monitor the local values.

          Comment


          • #6
            Originally posted by mloebl View Post
            Also this is what its for; http://www.uradmonitor.com/

            I have one of the radiation sensors and wanted to monitor the local values.
            If you used my method then I'm glad it worked, I did think it may have been something similar, my rudimentary college physics made me think cpm is clicks/counts per minute from some sort of radiation meter, nice to see I was not too far off...
            My Plugins:

            Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
            Yeelight 3P | Nanoleaf 3P

            Comment


            • #7
              I'm still struggling with this. I'm starting simple, reading the following string from my thermostat: {"api_ver":5,"type":"residential"}
              I can read it into 'Query' okay, but when I try to write the deserialized output, I get nothing. (I know there's easier ways to capture the result, but I want to move to more complex models once I get the hang of it.)

              The last few lines of my Settings.ini in the HomeSeer HS3\Config folder includes the reference to: "System.Web.Extensions.dll". I have not moved the dll to the HS3 folder, but System.Web.Extensions.dll is referenced in my tenScripting3 project. I've renamed most of my variables below to that of Mr. Happy's example, to make it easier for everyone to follow. I threw a few 'hs.WriteLogEx' lines in there just to see what the data was returning.
              Attached Files

              Comment


              • #8
                I don't in reality know why it is not working, but this should work;

                Code:
                Imports System.Web.Script.Serialization
                
                Dim json As New JavaScriptSerializer
                
                Sub Main(ByVal Parms As Object)
                
                    Try
                        Dim DataRaw As String = "{""api_ver"":5,""type"":""residential""}"
                
                        Dim jData As resultdata = json.Deserialize(Of resultdata)(DataRaw)
                
                        hs.writelog("", jData.api_ver)
                        hs.writelog("", jData.type)
                
                    Catch ex As Exception : hs.writelog("", "Exception: " & ex.message)
                    End Try
                
                End Sub
                
                Public Class resultdata
                    Public Property api_ver As String
                    Public Property type As String
                End Class
                My Plugins:

                Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
                Yeelight 3P | Nanoleaf 3P

                Comment


                • #9
                  Working script

                  Yes, Thank you Mr. Happy!
                  I simplified my script based on your format. I removed the items in yellow, and replaced the reference to 'jsonStructure' with the 'data' class (See screen capture image, attached). Text follows:
                  Code:
                   Imports System.Web.Script.Serialization
                  ClassVenstarT7900
                  PublicSub Main(ByVal Parms AsObject)
                   Dim Green AsString = "#008000"' Set WriteLogEx color Green
                  Dim Red AsString = "#FF0000"' Set WriteLogEx color Red
                  Try
                  Dim Query AsString = hs.GetURL("http://192.168.0.125", "/", False, 80) ' data String returned from Thermostat
                  hs.WriteLogEx("Query=", Query, Green) 'Query= {"api_ver":5,"type":"residential"} 
                  Dim json AsNewJavaScriptSerializer
                  Dim dataObj Asdata = json.Deserialize(Ofdata)(Query)
                   hs.WriteLogEx("dataObj=", dataObj.ToString, Green) 'dataObj= tenScripting3.VenstarT7900+jsonStructure 
                  hs.WriteLogEx("api_ver: ", dataObj.api_ver, Green) ' returns nothing
                  hs.WriteLogEx("type: ", dataObj.type, Green) ' returns nothing
                  Catch ex AsException
                  hs.WriteLogEx("VenstarAPI Error: ", ex.Message.ToString, Red)
                   EndTry
                  EndSub
                   
                    
                  PublicClassdata
                  PublicProperty api_ver AsString
                  PublicProperty type AsString
                  EndClass
                  EndClass
                  
                  Output to HS Log:
                  Jan-20 10:14:44 AM type: residential
                  Jan-20 10:14:44 AM api_ver: 5
                  Jan-20 10:14:44 AM dataObj= tenScripting3.VenstarT7900+data
                  Jan-20 10:14:44 AM Query= {"api_ver":5,"type":"residential"}
                  Attached Files

                  Comment


                  • #10
                    Hi,

                    I have a script that parse Json Data from web site

                    It create and put into 5 devices in HS3 the data From EDF TEMPO

                    https://particulier.edf.fr/bin/edf_r...peAlerte=TEMPO
                    https://particulier.edf.fr/bin/edf_r...peAlerte=TEMPO

                    You must update your system.ini with this reference:
                    ScriptingReferences=Newtonsoft.Json;C:\"Your REP HS3"\Bin\Newtonsoft.Json.dll
                    Jean-Francois.
                    Attached Files

                    Comment


                    • #11
                      I realize this is comically wrong... I'm not sure how to separate out the array. Can someone point me in the right direction?

                      Code follows, and I've attached a screen capture that's a little easier on the eyes (formatting).

                      Code:
                      PublicSub QueryAlerts(ByVal Parms AsObject)
                       Dim query AsString = hs.GetURL("http://192.168.0.125", "/query/alerts", False, 80) 
                       ' data String returned from Thermostat
                      ' query={"alerts":[{"name":"Air Filter","active":true},{"name":"UV Lamp","active":false},{"name":"Service","active":true}]}
                      Dim jss AsNewJavaScriptSerializer
                      Dim dataObj AsAlertType = jss.Deserialize(OfAlertType)(query)
                       
                        hs.WriteLog("Name:", dataObj.alerts.name)
                       hs.WriteLog("Active:", dataObj.alerts.active)
                      EndSub
                      
                       PublicClassAlertType
                      PublicProperty alerts AsList(OfAlertData)
                       EndClass
                      
                       PublicClassAlertData
                      PublicProperty name AsString
                      PublicProperty active AsBoolean
                      EndClass
                      
                       EndClass
                      
                      Attached Files

                      Comment


                      • #12
                        For each alert as PublicClassAlertData in dataObj.alerts
                        HS.writelog("JSON", alert.name)
                        Next



                        Sent from my iPhone using Tapatalk
                        Author of Highpeak Plugins | SMS-Gateway Plugin | Blue Iris Plugin | Paradox (Beta) Plugin | Modbus Plugin | Yamaha Plugin

                        Comment


                        • #13
                          Excuse simplicity and bad formatting - sent from a mobile.


                          Sent from my iPhone using Tapatalk
                          Author of Highpeak Plugins | SMS-Gateway Plugin | Blue Iris Plugin | Paradox (Beta) Plugin | Modbus Plugin | Yamaha Plugin

                          Comment


                          • #14
                            Thank you - I'm anxious to give it a go. I appreciate it!

                            Comment


                            • #15
                              Originally posted by mrhappy View Post
                              I don't in reality know why it is not working, but this should work;

                              ...
                              I would just like to say that I found this very helpful.
                              Thanks,

                              Comment

                              Working...
                              X