Reading/Understanding JSON to set a variable

Ahh. this =


1 Like

Thank You!..

Back at it…

This has got me stumped.

Part of a rule will set a variable using the total precipitation for the day using Open Weather 3.0


What is returned is this:

Using JSON Path Finder I get this:


The precipitation for the day at this time is 0.7

The variable is being set at 0

Am I using the wrong path to get

It appears there is no “value”… ???

I would take a look at the following HTTP Action Troubleshooting guide:

Especially since you are using variables in the URL, I would check the Rule Logs to make sure that the correct URL is being called.

I would also check the raw $ to see what response body you are getting and snapshot the $context.response.status for troubleshooting to make sure you are getting a valid response code.

1 Like

So it appears this works:{MY KEY}

But this does not:{{$DateToday}}&appid={MY KEY}

The difference is I put today’s date directly in the URL instead of the variable $DateToday

Does something need to go before or after inserting the variable?

Did you check what the generated URL was in your Rule Logs?

I just tested with a sample variable with a request to HTTP Bin and it looks like the URL works as expected.

I can see in my rule logs that the URL is exactly what I would expect it to be.

I would make sure that the variable has exactly what you expect in it. No less. No more. In other words, you want to make sure there aren’t any spaces or any unexpected characters.

Looking back at your original post where you were asking about being able to inject a variable into a URL, it looks like the sample format string you provided has a space at the end of it, so you’ll want to make sure that doesn’t exist otherwise you’ll get a space in the resulting variable and that space will ultimately get included in the URL as well:

I guess I was so focused on the current rule that I overlooked the rule that sets the date. There was a space before the end ".

Now it works. Thank you .

1 Like

I have a rule that sets the rain in the last hour.

The variable is set using this expression:

positions = $
isEmpty(positions) ? 0 : positions

The path is taken from JSON path finder:

I’m getting an error, and looking at the logs, it seems the “h” is missing from the expression. Is this my problem? I replaced the expression with the above, but the “h” keeps getting removed.


Variables must start with a letter (reference). I suspect that JSON Path just didn’t consider that with their design.

In order to reference a property which starts with a number, you would have to use the bracket notation.


Pasted exactly that, but it gave me the same result:


Are you certain that the response you are receiving within the rule execution has a rain field?

I would again recommend following the troubleshooting steps in the HTTP Action Troubleshooting guide that I linked above.

If I remember correctly, the Open Weather API does not always include a rain field. In that case, you would likely want to use the same type of approach you are using with the isEmpty() check and using a fallback as needed… perhaps in multiple steps.

Well, I was… It was in the JSON path finder. Looking back at the history, we’ve not had rain in the last 2 hours, so it does not appear now. I added the [“1h”] for future, but my current fallback does not seem to handle the missing field.

positions = $[“1h”]
isEmpty(positions) ? 0 : positions

The rain field itself might be missing. Trying to access that field in your first line will cause the expression evaluation error. You would need to use the same type of isEmpty() check to see if that field exists or fallback to a default.

Something like the following.

current = $
rain = isEmpty(current.rain) ? {} : current.rain
isEmpty(rain["1h"]) ? 0 : rain["1h"]

Or if this feels more logical to you, it’s the same result

current = $
isEmpty(current.rain) ? 0 : 
  isEmpty(current.rain["1h"]) ? 0 : 

It’s basically a nested IF / ELSE IF type of concept. The approach is explained at the bottom of the General Functions in Rule Expressions - SharpTools Knowledge Base article under the ‘Conditions / Ternary Expressions’ heading.

1 Like


It is missing, because of no rain currently.

I was thinking along these lines, but I was doing it backwards! It works… Much thanks… AGAIN!!

1 Like