Skip to content

JSON Transformations

pgfeller edited this page Feb 15, 2015 · 2 revisions

It is possible to parse JSON data in several ways; this page shall give some examples on how to do this.

Rules

Below example shows how to parse a JSON message received via MQTT in a rule (OH 1.6.2):

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "MqttPostionPatrikParse"
  when 
    Item mqttPositionPatrikRaw changed
  then
        var String json = (mqttPositionPatrikRaw.state as StringType).toString
	// {"_type": "location", "lat": "47.5010314", "lon": "8.3444293",
	//    "tst": "1422616466", "acc": "21.05", "batt": "40"}
	var String type = transform("JSONPATH", "$._type", json)
	if (type == "location") {
	  var String lat  = transform("JSONPATH", "$.lat", json)
	  var String lon  = transform("JSONPATH", "$.lon", json)
	  var String acc  = transform("JSONPATH", "$.acc", json)
	  var String batt = transform("JSONPATH", "$.batt", json)
	
      sendCommand(mqttPatrikLatitude,  lat)
	  sendCommand(mqttPatrikLongitude, lon)
	  sendCommand(mqttPatikAccuracy,   acc) 
	  sendCommand(mqttHtcOneBattery,  batt)
	}
  end

feature overview

Installation

Setup intro

Linux and OS X

Windows

FreeBSD


Configuration

Automation*

User Interfaces

Persistence


Community

(link to openHAB forum)

Development


Bindings


Application Integration

Misc


Samples

A good source of inspiration and tips from users gathered over the years. Be aware that things may have changed since they were written and some examples might not work correctly.

Please update the wiki if you do come across any out of date information.

Binding configurations

Use case examples

Item definition examples

Sitemap definition examples

Collections of Rules on a single page

Single Rules

Scripts

Syntax highlighting for external editors


Release Notes

Clone this wiki locally
You can’t perform that action at this time.