New variables feature: can I set one to an object or array with a template?

I have a case where I want to set a variable to be an object using a template, but instead I am getting a string that contains the object as a JSON string. I have the same problem if I want to set a variable to an array. I just get a JSON string of the array.

My intent is to reduce verbosity (and repetitiveness) of logic in other templates. Does anyone have any ideas?

Object example:

  data: "{{}}"
  #this next line fails because data is a JSON string rather than the desired object
  value: "{{data.value if data.value is string else data.value|random if data.value}}" 

Array example:

  message: "{{}}"
  #message is always a string even if was an array
  value: "{{message if message is string else message|random if message}}"

As far as I understand, a Jinja template always outputs a single string. So you’d need to make a variable for each field in the data block for this to work.

I use input select as data arrays

@Burningstone, that’s what I ended up doing. I wonder if the parser for the new variables feature could be enhanced to recognize JSON and deserialize it automatically?

@lonebaggie, that’s an interesting technique. I can’t use it in this case because the values cannot be global, but I will keep it in mind if I run into a scenario where it will help.

I submitted a feature request and found out that exactly what I want is in a PR. Hopefully it gets merged!

Here is is my feature request, and the first comment has a link to the PR.