JsonPath Transformation & Exponential notation

Hello,

I’m doing a JsonPath Transformation like this in DSL.

var String resultHTTP = transform("JSONPATH",""$.data[0].tuples[0][1]","{ "version": "0.8.3", "generator": "vzlogger", "data": [ { "uuid": "111", "last": 111, "interval": -1, "protocol": "sml", "tuples": [ [ 1701498445280, 34746388.600000001 ] ] } ] }") as String

Aa a result I get “3.47463886E7”, but actually it should be “34746388.600000001”. Why is it like this, and how can I get the “34746388.600000001”?

I guess it’s scientific notation by default. Use a Number (or maybe Double) variable instead:

var Double dEnergy = transform("JSONPATH","$.data[0].tuples[0][1]","{ "version": "0.8.3", "generator": "vzlogger", "data": [ { "uuid": "111", "last": 111, "interval": -1, "protocol": "sml", "tuples": [ [ 1701498445280, 34746388.600000001 ] ] } ] }")

Please be aware that the line of code is not valid as the quotes have to be escaped.

By the way: I’m using volkszähler, too, and I get the raw values via mqtt rather than http. Can be configured in vzlogger.conf, I’m getting one topic per channel, like this:

This does not work. I got this error. JSONPATH seems to create a String with scientific notation?! Any further ideas?

 Type mismatch: cannot convert from String to Double;

I also thought about MQTT. I do not have Mosquitto installed yet, but this should not be the issue. My problem is rather that MQTT would send e.g. every 1 minute, but I would only persist every hour at xx:59:30. So there is a possible time gap of max. 1 minute between MQTT value and persisting. Did you solve this issue? At the moment I think it is not possible to persist with a timestamp.

Please trout this:

var dEnergy = transform('JSONPATH','$.data[0].tuples[0][1]','{ "version": "0.8.3", "generator": "vzlogger", "data": [ { "uuid": "111", "last": 111, "interval": -1, "protocol": "sml", "tuples": [ [ 1701498445280, 34746388.6 ] ] } ] }') as Double

or as an alternative:

val json = "{\"version\": \"0.8.3\", \"generator\": \"vzlogger\", \"data\": [ { \"uuid\": \"111\", \"last\": 111, \"interval\": -1, \"protocol\": \"sml\", \"tuples\": [ [ 1701498445280, 34746388.6 ] ] } ] }"
val path = "$.data[0].tuples[0][1]"
var Double dEnergy = Double.parseDouble(transform("JSONPATH",path,json))

I have done some testing. The JSONPath transform seems to always do an internal conversion with scientific notation and returns a String back. The problem here is that there is a loss of precision. Why does this happen?

Of course I can convert the returned String to Double and then back to String like this, but as said only with loss of precision.

String::format("%.4f", Double.parseDouble(resultHTTP) )

Do you know where I can find the JSONPath transform in the Openhab code? Would be interesting what happens here.

In fact, even the “34746388.600000001” is not correct (no, vzlogger can’t measure sub-sub Watts where there aren’t)
But the behavior of transform seems to be very strange. I’ve checked here and I can confirm.

Did you tryout a direct transformation yet? (i.e. build a channel and convert directly in the channel)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.