0
Answered

http basic string manipulation

Andy Gill 3 years ago in Devices / Http updated by Joachim 10 months ago 12

Hi There,


Usinng the Http basic I can bring a string back from an API to another device, but that string contains data I do not need, I only want the present value from this response: {"present-value":22.285308837890625,"object-id":"0.1001","object-instance":"1001","object-type":"0","device-id":"0","href":"http://192.168.0.100:47800/api/v1/bacnet/devices/0/objects/0.1001"}


Is this possible in Comfort Clik, in short I wanto to populate a temperature in comfort click using the present value in the above response?


Thanks


Answer

Answer

Hello,

it is possible to parse JSON and XML data from the response. If your response is in one of those formats it is quite easy to read out the data. Another way is to use REGEX and parse out any data from text, but that is harder.


Example for JSON:

{

"Sensor1": {
"Temperature": 10
}

}

On the main HTTP node chose parser type "Json"

If you would like to read out temperature from sensor 1 (value 10), create a new variable under HTTP node e.g. double and enter Sensor1.Temperature in the Token Name field.


Regards


GOOD, I'M SATISFIED
Satisfaction mark by Andy Gill 3 years ago
Answer

Hello,

it is possible to parse JSON and XML data from the response. If your response is in one of those formats it is quite easy to read out the data. Another way is to use REGEX and parse out any data from text, but that is harder.


Example for JSON:

{

"Sensor1": {
"Temperature": 10
}

}

On the main HTTP node chose parser type "Json"

If you would like to read out temperature from sensor 1 (value 10), create a new variable under HTTP node e.g. double and enter Sensor1.Temperature in the Token Name field.


Regards


Thanks that worked a treat.

One more problem I've got another device I would like to get data from it returns the follwoing JSON:

{

  • ActTime: 1477130917,
  • ServerTime: "2016-10-22 11:08:37",
  • Sunrise: "07:54",
  • Sunset: "18:06",
  • result:
    [
    • {
      • AddjMulti: 1,
      • AddjMulti2: 1,
      • AddjValue: 0,
      • AddjValue2: 0,
      • BatteryLevel: 255,
      • CustomImage: 0,
      • Data: "53.0 C",
      • Description: "",
      • Favorite: 0,
      • HardwareID: 2,
      • HardwareName: "Device Performance",
      • HardwareType: "Motherboard sensors",
      • HardwareTypeVal: 23,
      • HaveTimeout: false,
      • ID: "03EA",
      • LastUpdate: "2016-10-22 11:08:18",
      • Name: "CPU Core 1",
      • Notifications: "false",
      • PlanID: "0",
      • PlanIDs:
        [
        • 0
        ],
      • Protected: false,
      • ShowNotifications: true,
      • SignalLevel: "-",
      • SubType: "LaCrosse TX3",
      • Temp: 53,
      • Timers: "false",
      • Type: "Temp",
      • TypeImg: "temperature",
      • Unit: 234,
      • Used: 1,
      • XOffset: "0",
      • YOffset: "0",
      • idx: "3"
      }
    ],
  • status: "OK",
  • title: "Devices"

}


I'm looking to get the Temperature which is labelled Data in the JSON list so my double is "result.Data" is this correct as I'm not getting a value in the double? I do have a response data in the command though.


Not sure what I'm missing?

OK Worked it out in the end: result[0].Data

Hi,

I have a similar situation and I am struggling to extract the minutes value from the string.

For example the value:08 from the following ServerTime: "2016-10-22 11:08:37".

Do you have any tips on how this can be done?

Thank you,

Calin

you can use following regex for that line : (?<=\:)\d+(?=\:)

Regards,

Hi Fabien,

Thank you so much for your guidance.

I have tried the regex (?<=\:)\d+(?=\:) and I didn't get any result in bOS.

After further investigations and trying the online regex tester I assumed the reason for not returning any value in bOS is because there are multiple matches in the string.

I came up with a new regex to read the minutes value only from the line with "thermostatTime"

(?<=\"thermostatTime\"\:\s\"\d+\-\d+\-\d+\s\d+\:)\d+

here is a portion of the JSON response:

  "thermostatList": [

    {

  "name": "Main",

  "features": "Home,HomeKit",

      "lastModified": "2019-01-10 02:25:01",

      "thermostatTime": "2019-01-11 18:29:56",

      "utcTime": "2019-01-11 23:29:56",

      "runtime": {

        "runtimeRev": "190111232705",

        "connected": true,

      }

    }

  ],

  "status": {

    "code": 0,

    "message": ""

  }

}

It works on the regex tester but no value is returned in bOS.

Any thoughts on what would be the reason that is not working in bOS? Am I missing something?

Thank you,

Calin

Hello,

Just realized I forgot to mention that the REGEX is only working if you parse text data. Your regex expression should be working as soon as the full answer is received in text format.

If you go and use the JSON format, like you do in your exemple, you will only be able to extract the token value (here it would be 2019-01-11 18:29:56


Try to change the parser type from JSON to text... As I don't see all details of your device I'm not sure if your receiver will get it, but I've made some test with a "basic" type device and it worked. I've extracted the value your are looking for.

answer received in text format


test regex

integer answer value based on my test message

I hope it helps

Hi Fabien,

After changing the parse type from JSON to text I was able to extract only the minutes value using regex. Problem SOLVED!!!

Thanks again for your help.
 

Nice to read you could fix it Calin !

Hijacking this thread.

I'm trying to get the 589 value from "duration_in_traffic" with token below:


rows[0].elements[0].duration_in_traffic.@value

Not having any luck.

JSON Response:

 

   "rows":[  
  {  
 "elements":[  
        {  
               "distance":{  
                  "text":"7.1 km",
                  "value":7073
               },
               "duration":{  
                  "text":"10 mins",
                  "value":595
               },
               "duration_in_traffic":{  
                  "text":"10 mins",
                  "value":589
               },
               "status":"OK"
            }
         ]
      }
   ],
   "status":"OK"
}

Suggestions anyone?

EDIT: removed the @. Worked like a charm :)