Announcement

Collapse
No announcement yet.

Reading a JSON data object which includes an array

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

    Reading a JSON data object which includes an array

    I have a SolarEdge inverter and have had some success in reading simple JSON data from the web api. However, I would like to get more data but I don't know how to.
    The data is below and I want to read in the value for each type of meter. eg I want the "Purchased" value of 3075.0, SelfConsumption value of 11602.0.

    I script in vb.net - please can anyone help?

    {
    "energyDetails":{
    "timeUnit":"DAY",
    "unit":"Wh",
    "meters":[
    {
    "type":"Purchased",
    "values":[
    {
    "date":"2019-06-24 00:00:00",
    "value":3075.0
    }
    ]
    },
    {
    "type":"SelfConsumption",
    "values":[
    {
    "date":"2019-06-24 00:00:00",
    "value":11602.0
    }
    ]
    },
    {
    "type":"Production",
    "values":[
    {
    "date":"2019-06-24 00:00:00",
    "value":26566.0
    }
    ]
    },
    {
    "type":"FeedIn",
    "values":[
    {
    "date":"2019-06-24 00:00:00",
    "value":15089.0
    }
    ]
    },
    {
    "type":"Consumption",
    "values":[
    {
    "date":"2019-06-24 00:00:00",
    "value":14672.0
    }
    ]
    }
    ]
    }
    }

    #2
    See if this helps

    https://forums.homeseer.com/forum/de...r-script/page2

    Comment


      #3
      I found this very helpful too.
      https://forums.homeseer.com/forum/ho...39#post1140739

      Comment


        #4
        If you want an off-the shelf solution, you can use my datascraper package to do this. I can provide you with help with the patterns if you get stuck.
        Jon

        Comment


          #5
          Originally posted by jon00 View Post
          If you want an off-the shelf solution, you can use my datascraper package to do this. I can provide you with help with the patterns if you get stuck.
          Thanks Jon. I've actually done it the old fashioned way using instr and mid. I was hoping that the JSON format was going to be easy to deserialise (if that's the right term) but it seems not. As the output from the SolarEdge API won't change I'm confident it'll do the job.

          Comment


            #6
            Originally posted by Jez View Post

            Thanks Jon. I've actually done it the old fashioned way using instr and mid. I was hoping that the JSON format was going to be easy to deserialise (if that's the right term) but it seems not. As the output from the SolarEdge API won't change I'm confident it'll do the job.
            That of course will work! If you'd like proper JSON parsing try JsonConvert.DeserializeObject (may have to include it as it is the Newtonsoft library). Arrays [1] and [2] below represent the fact that Purchased and SelfConsumption are the [1] and [2] index (2nd and 3rd) in the meters array.

            https://stackoverflow.com/questions/...arse-in-vb-net
            Dim data As Object = JsonConvert.DeserializeObject(Of Object)(json)

            Dim purchased As string = data("energyDetails")("meters")(1)("values")("value")
            Dim selfConsumption As string = data("energyDetails")("meters")(1)("values")("value")

            Comment


              #7
              Originally posted by gpez View Post

              That of course will work! If you'd like proper JSON parsing try JsonConvert.DeserializeObject (may have to include it as it is the Newtonsoft library). Arrays [1] and [2] below represent the fact that Purchased and SelfConsumption are the [1] and [2] index (2nd and 3rd) in the meters array.

              https://stackoverflow.com/questions/...arse-in-vb-net
              Dim data As Object = JsonConvert.DeserializeObject(Of Object)(json)

              Dim purchased As string = data("energyDetails")("meters")[1]("values")("value")
              Dim selfConsumption As string = data("energyDetails")("meters")[2]("values")("value")

              Thanks very much for that. I did want to be able to parse the JSON and thought it should be easy but didn't know the syntax.
              I've got it working but I had to replace the [] brackets with () and theres also another array at the end (it would contain multiple dates if myAPI call had specified a larger time period). So, if anyone else is struggling, the vb.net code is:

              Dim data as object = JsonConvert.DeserializeObject(jsonText)

              Dim purchased As string = data("energyDetails")("meters")(1)("values")(0)("value")
              Dim selfConsumption As string = data("energyDetails")("meters")(2)("values")(0)("value")

              Edit: I realised that the purchased value isnt always array 1, selfConsumption isn't always array 2 so I've had to do a little more coding. I'm reading the JSON data back into an array and using if... then on each record. Its not ideal but it works.

              Comment


                #8
                Originally posted by Jez View Post

                Thanks very much for that. I did want to be able to parse the JSON and thought it should be easy but didn't know the syntax.
                I've got it working but I had to replace the [] brackets with () and theres also another array at the end (it would contain multiple dates if myAPI call had specified a larger time period). So, if anyone else is struggling, the vb.net code is:

                Dim data as object = JsonConvert.DeserializeObject(jsonText)

                Dim purchased As string = data("energyDetails")("meters")(1)("values")(0)("value")
                Dim selfConsumption As string = data("energyDetails")("meters")(2)("values")(0)("value")

                Edit: I realised that the purchased value isnt always array 1, selfConsumption isn't always array 2 so I've had to do a little more coding. I'm reading the JSON data back into an array and using if... then on each record. Its not ideal but it works.
                Good catch - as a C# developer switching back to VB sometimes trips my syntax up. I've updated the post and am glad that it's working.

                Comment


                  #9
                  Thanks again. I knew it would be possible, but as an amateur scripter I just couldn’t find the syntax needed. This forum is so useful.

                  Comment

                  Working...
                  X