JSON Transformation Help needed

Hi There,

from a webservice i get the following answer

response={"ok":true,"license":"CC BY 4.0 - http:\/\/creativecommons.tankerkoenig.de","data":"MTS-K","prices":{"1ceea95b-d46e-4159-91f6-71f57d612bdb":{"status":"open","e5":1.299,"e10":1.279,"diesel":1.059}}}

How should the *.js file structured to get the value for diesel?



I think JSON.parse(input).prices["1ceea95b-d46e-4159-91f6-71f57d612bdb"].diesel; should do the trick. Assuming 1ceea95b-d46e-4159-91f6-71f57d612bdb does not change.


Thats the thing the “1ceea…” changes for different stations. additinally i could put different stations into one request, but then i have no idea how to split the data into different items

the response with two ids is like that:
{"ok":true,"license":"CC BY 4.0 - http:\/\/creativecommons.tankerkoenig.de","data":"MTS-K","prices":{"e1a15081-24e2-9107-e040-0b0a3dfe563c":{"status":"open","e5":1.319,"e10":1.299,"diesel":1.079},"1ceea95b-d46e-4159-91f6-71f57d612bdb":{"status":"open","e5":1.309,"e10":1.289,"diesel":1.069}}}

at the end the question is how do i get 5 prices into five different items. actual a item is like that:

Number InVl_DieselPrice1_Value "1. Dieselpreis [%.2f €]" <scene_gas_station> (gTankerkoenig, gFuelPrices) { http="<[https://creativecommons.tankerkoenig.de/json/prices.php?ids=3d8c2dfb-4612-4aff-8ae7-96e3f88eafe6&apikey=xxxxxx:300000:JS(diesel.js)]" }


Multiple price values in one request should be possible by using the cache feature of the HTTP binding, see https://github.com/openhab/openhab/wiki/Http-Binding#caching

Then you create one Number item for each station, taking the cached result and in the diesel.js you need to pick the right value using the “1ceea…” id. Assuming this is a unique identifier for a gas station it should not change.

Fixed it.

Thanks a lot

Can you share your solution?


Hi Andreas,

ich will create a new topic with all the files and rules and …


In order to get prices from multiple gas-stations in one JSON-string you should use the (new) prices.php script as posted on the website. Works great.
…and, why don’t you use JSON Transformation Service in order to directly get the needed values? No extra file needed!

Look at my other (solved) questions, I’m rght at that topic.

@opus I use the new prices script and yes it works great. Could you give a example how to use the JSON Transformation Services.

Could you provide a link to your solutions?


Look at that thread, last posting:

I’m at work posting from my mobile, sorry for the "format"of the link.

No problem, i’m at work too.

OK i understand the approach. This could reduce the nnumber of item i use. Is it right that the json at the of the statement holds the complete json string. How do you fill this?


The part for getting the json is in this thread

If you wait until later today I’ll post my rule which gets all prices of three fixed stations every 5 minutes.

That moves the solution forward to more elegance :slight_smile:


I will come back with my complete solution after rebuiding it


Although my solution is running now, my head is working on reducing the number of requests.
I do read the details of the station once a day, that way I have the opening times. However I do check every 5 minutes for a price update, even if the station is closed. So there is room for improvement!

Currently the opening times are not of interest, because usually stations are open while i am awake :slight_smile:

What is your concern about the number of requests? You could pause the check during the night when ou are asleep. Eg between 11:00 and 4:00 in the morning what ever suites you best. Or you can go down to check every 10 minutes. I think that depaends on you behaviour. Do you check price and based on the price you go to the station or is it more like “I’m on the way lets see if gas is cheap and if so let refuel”


Ah, sorry, didn’t tell!
My main approach is to chart the prices for day/week/4week period using a RoundRobinDB. Works well!

OK and why just for fun?

Just for the users on my openhab server.

This my rule:

import org.openhab.core.library.types.*
var String Tankstelle1_ID=""
var String Tankstelle2_ID=""
var String Tankstelle3_ID=""
var String API_Key=""
var String Status
var Number E10
var Number E5
var Number Diesel

rule "Benzinpreise"
    Time cron "0 0/5 * * * ?"
    var String URL= "https://creativecommons.tankerkoenig.de/json/prices.php?ids=" + Tankstelle1_ID + "\," +Tankstelle2_ID + "\," + Tankstelle3_ID + "&apikey=" + API_Key
    //logInfo ("Benzinpreise", "URL = {}", URL)
    var String json = sendHttpGetRequest(URL)
    //logInfo("Benzinpreise","JSON-String = {}", json)
    var String OK = transform("JSONPATH", "$.ok", json)
    //logInfo("Benzinpreise", "OK = {}", OK)
    if (OK=="true") {
        Status = transform("JSONPATH", "$.prices." + Tankstelle1_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if (Status=="open") {
            E10 = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".e10", json)
            E5  = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".e5", json)
            Diesel  = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".diesel", json)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
        else {
            logInfo("Benzinpreis", "Status = {}", Status)
        Status = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if (Status=="open") {
            E10 = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".e10", json)
            E5  = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".e5", json)
            Diesel  = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".diesel", json)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
          else {
            logInfo("Benzinpreis", "Status = {}", Status)
        Status = transform("JSONPATH", "$.prices." + Tankstelle3_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if(Status=="open") {
            E10 = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".e10", json)
            E5  = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".e5", json)
            Diesel  = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".diesel", json)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
        else {
            logInfo("Benzinpreis", "Status = {}", Status)
    else {
        var String HttpError = transform("JSONPATH",  "$.message", json)
        logInfo("Benzinpreis", "HttpError = {}", HttpError)

I’m commented out all loggins if the request runs normal.
E10_1, E10_2, E10_3, … are items on the sitemap, they are persitet in a rrd4j database.
You need to set the strings for API_Key and the 3 gas-station IDs

Do you operate it on OH 1.8.3? With OH2 i get the error that the transform returns a String not a Number