HTTP Binding Parse JSON response

1,517 views
Skip to first unread message

Chris Dunn

unread,
Nov 26, 2013, 5:32:38 PM11/26/13
to ope...@googlegroups.com
I'm issuing a query to my DirecTV receiver with the HTTP bind. The response is in JSON format. At the end is an example of the response. I have attempted to use a REGEX to get the title info from the response but it looks like it dons't like the additional quote marks. Here's an example: 

String DirecTV1_Channel "Current Channel" { http="<[http://10.90.30.100:8080/tv/getTuned:30000:REGEX(/"title": "([^"]+)/)]" }

If I use that REGEX expression in a REGEX test tool with the JSON data it extracts the title. Is there a way to work around the quote marks or a better way of doing this? Any guidance would be great.

-Chris

Example Response
{
  "callsign": "KSBW",
  "date": "20131126",
  "duration": 3600,
  "isOffAir": false,
  "isPclocked": 3,
  "isPpv": false,
  "isRecording": false,
  "isVod": false,
  "major": 8,
  "minor": 65535,
  "offset": 3157,
  "programId": "11269591",
  "rating": "No Rating",
  "startTime": 1385485200,
  "stationId": 3910459,
  "status": {
    "code": 200,
    "commandResult": 0,
    "msg": "OK.",
    "query": "/tv/getTuned"
  },
  "title": "Today"
}

klus...@gmail.com

unread,
Feb 16, 2014, 1:42:17 PM2/16/14
to ope...@googlegroups.com
Hi!

I was facing similar challenge and I've fixed it by making the REGEX actually parse the values I was interested in out of JSON. My JSON string looks like this:



{"ledstripe.temp": ["19.820", 1392568932.927889], "diningroom.temperature": ["20.040", 1392568933.346824], "lamp.mainlightstat": ["0.000", 1392568932.920869], "lamp.colour_red": ["0.000", 1392568928.110235], "lamp.colour_blue": ["0.000", 1392568928.103455], "kitchen.fridgetemp": ["4.200", 1392568933.353668], "lamp.colour_green": ["0.000", 1392568928.117113], "kitchen.freezertemp": ["-25.740", 1392568933.36056]}

And corresponding parsers are following: 

Number Temp_Livingroom "Living room [%.1f]" <temperature>  (IND_LivingRoom) { http="<[http://192.168.2.202:7777/views/helloworld/sensors_valid:5000:REGEX(.*?ledstripe.temp\": \\[\"(.*?)\",(.*))]" }
Number Temp_KitchenAmbient "Room [%.1f]" <temperature>  (IND_Kitchen) { http="<[http://192.168.2.202:7777/views/helloworld/sensors_valid:5000:REGEX(.*?diningroom.temperature\": \\[\"(.*?)\",(.*))]" }Number Temp_Fridge "Fridge [%.1f]"     <temperature>  (IND_Kitchen) { http="<[http://192.168.2.202:7777/views/helloworld/sensors_valid:5000:REGEX(.*?kitchen.fridgetemp\": \\[\"(.*?)\",(.*))]" }Number Temp_Freezer "Freezer [%.1f]"    <temperature>  (IND_Kitchen) { http="<[http://192.168.2.202:7777/views/helloworld/sensors_valid:5000:REGEX(.*?kitchen.freezertemp\": \\[\"(.*?)\",(.*))]" }

Jan-Piet Mens

unread,
Feb 17, 2014, 6:02:59 AM2/17/14
to ope...@googlegroups.com
> I was facing similar challenge and I've fixed it by making the REGEX
> actually parse the values I was interested in out of JSON.

You cannot safely parse JSON with a regex. It will break some day,
probably sooner than later. :)

-JP

klus...@gmail.com

unread,
Feb 17, 2014, 6:31:21 AM2/17/14
to ope...@googlegroups.com
You are absolutely correct! I forgot to add that I am generating the JSON myself and thus have full control over its contents, which makes it possible to create both JSON-valid output and regex-parseable output.

However, to further elaborate on the issue - it is possible to create a REGEX that will parse a JSON file (given limited sub-set of functionality required), it is just not very nice or simple regex to be used:

Jens Kluge

unread,
Feb 17, 2014, 6:41:32 AM2/17/14
to ope...@googlegroups.com
Hi Stefan,
>
> Bringt das BIOS mod neben hotswap noch andere
> Vorteile mit sich?
ja; ist aber kein "richtiges" mod sondern nur die Freischaltung von "hidden" Parameter im bios

Folgender Link zu deinen Fragen:
http://www.hardwareluxx.de/community/f101/hp-proliant-n36l-n40l-n54l-microserver-752079-430.html#post21503128

BIOS Update ist völlig ungefährlich, aber das modifizierte BIOS ist nur schwer zu finden, deshalb hier gleich als Anhang, wenn du es brauchen solltest

Grüße
Jens
--


____________________
Sicher ist sicher! E-Mail-Verkehr nur noch verschluesselt (SSL)!

O41100113.zip

mgol...@gmail.com

unread,
Apr 6, 2014, 6:35:22 PM4/6/14
to ope...@googlegroups.com
Probably too late, but anyway. Handling JSON format is much easier with the JavascriptTransformationService.
You define your item like 
String DirecTV1_Channel "Current Channel" { http="<[http://10.90.30.100:8080/tv/getTuned:30000:JS(getValue.js)]" }

Then you put a file getValue.js in $OPENHAB_DIR/configuration/transform/

The content of getValue.js is:
JSON.parse(input).title;

That's it.

--
Martin

Petr Klus

unread,
Apr 24, 2014, 4:03:17 PM4/24/14
to ope...@googlegroups.com
Very useful!

Any way to pass a query parameter to the JS? For example, have a single JS file that translates the JSON and I would be then able to specify field name in the item, instead of creating .js file for each possible variable


--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/bdTDlK2GDNI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

Gert Konemann

unread,
Apr 24, 2014, 6:15:33 PM4/24/14
to ope...@googlegroups.com
I have no experience with JSON, but a few of my devices use it. Can someone make a few samples in the Samples Wiki ? That would probably help not only me.

Gert

Op donderdag 24 april 2014 22:03:17 UTC+2 schreef Petr Klus:
Reply all
Reply to author
Forward
0 new messages