Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Jsonpath transformation doesn't work with null values in json data. #4870

Closed
pacive opened this issue Jan 7, 2018 · 7 comments · Fixed by #4909
Closed

Jsonpath transformation doesn't work with null values in json data. #4870

pacive opened this issue Jan 7, 2018 · 7 comments · Fixed by #4909

Comments

@pacive
Copy link

pacive commented Jan 7, 2018

Since commit f1c63ba7177bf007b457f60676c8701e87c74343 the jsonpath transformation no longer work properly when the json data contains null values. Instead of returning null it returns the whole json string when accessing a key with null value.

For example when trying to access the 'upgrade' field in
{"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}
the whole string is returned instead of null.

Steps to reproduce:

rule "test"
	when
		Item Test changed
	then
		var json1 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":\"null\"}"
		var json2 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":null}"
		logInfo("Test", transform("JSONPATH", "$.upgrade", json1))
		logInfo("Test", transform("JSONPATH", "$.upgrade", json2))
end

returns

2018-01-03 15:07:18.458 [INFO ] [.eclipse.smarthome.model.script.Test] - null
2018-01-03 15:07:18.465 [INFO ] [.eclipse.smarthome.model.script.Test] - {"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}

More info in this post on the OpenHAB forum.

@triller-telekom
Copy link
Contributor

The change was indeed in this line as you mentioned in the linked openHAB forum post.

But the problem is a few lines above

Object transformationResult = JsonPath.read(source, jsonPathExpression);

We cannot tell whether the received value from the read call is really null or if there was an error.

@pacive
Copy link
Author

pacive commented Jan 9, 2018

I which cases does an error in the JsonPath.read() lead to a null value being returned? If I try to access a nonexistent field for example, it throws a PathNotFoundException which is handled and a TransformationException is thrown, instead of returning null as it previosly did.

rule "test"
	when
		Item Test changed
	then
		var json1 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":\"null\"}"
		var json2 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":null}"
		logInfo("Test", transform("JSONPATH", "$.upgrade", json1))
		logInfo("Test", transform("JSONPATH", "$.upgrade", json2))
                logInfo("Test", transform("JSONPATH", "$.someField", json2))
end

logs:

2018-01-09 15:57:10.505 [INFO ] [.eclipse.smarthome.model.script.Test] - null

2018-01-09 15:57:10.515 [INFO ] [.eclipse.smarthome.model.script.Test] - {"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}

2018-01-09 15:57:10.525 [ERROR] [ore.transform.actions.Transformation] - Error executing the transformation 'JSONPATH': Invalid path '$.someField' in '{"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}'

2018-01-09 15:57:10.534 [INFO ] [.eclipse.smarthome.model.script.Test] - {"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}

@triller-telekom
Copy link
Contributor

Thank you for trying this out with an example. I am wondering this is always the case? I could not find any documentation for this behavior, did you come across any?

@pacive
Copy link
Author

pacive commented Jan 9, 2018

Just the JsonPath readme. It can be configured to return null if an exception occurs, but that isn't the case in this implementation, so i don't think itshould return null unless the requested field is null.

@kaikreuzer or @SJKA, any more input on this?

@triller-telekom
Copy link
Contributor

Sounds like what we need. Do you want to come up with a PR for this?

@sjsf
Copy link
Contributor

sjsf commented Jan 12, 2018

As you can see from the discussion in #4379, we were all a little unsure what to return in that case. With your analysis it indeed would make sense to define null as a valid outcome. If the element doesn't exist it will still throw the TransformationException.

It can't be the real null though, it has to be the string representation of UnDefType.NULL, i.e. "NULL"

sjsf added a commit to sjsf/smarthome that referenced this issue Jan 12, 2018
...in case the element really exists and is set to NULL.

fixes eclipse-archived#4870
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
@pacive
Copy link
Author

pacive commented Jan 12, 2018

That would solve my problem, just need to change null to "NULL" in my rule. Thanks @SJKA!

maggu2810 pushed a commit that referenced this issue Jan 14, 2018
...in case the element really exists and is set to NULL.

fixes #4870
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
dgajic pushed a commit to dgajic/smarthome that referenced this issue Jan 27, 2018
…-archived#4909)

...in case the element really exists and is set to NULL.

fixes eclipse-archived#4870
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants