Converting string to integer in Logic-App Liquid Map - json

Im trying to convert a string to an integer inside Logic-App Liquid maps.
My Liquid is like this
{% assign var = "2" %}
{% assign number = var| Plus: 1 %}
{
"number":"{{number}}"
}
And the output (below) is calculated as 21 (string concatination) and not 3 as expected.
{
"number": "21"
}
Any suggestion on how to change this string to an Integer so I can do mathematical calculations on it would be appreciated.
Thanks

According to some test, it seems {% assign number = var | Plus: 1 %} can't convert the string to integer when we run it in logic app liquid (although it works in liquid outside logic app, as many websites and posts mentioned on internet).
But we can do it by below liquid template in logic app:
{% assign var = "2" %}
{% assign var1 = var | Round %}
{% assign number = var1 | Plus: 1 %}
{
"var1":{{var1}},
"number":{{number}}
}
It works fine in my logic app and below is the screenshot of the result:

Related

Build queries from Jekyll csv data file

So I have a csv file with rows of data called "opportunities.csv"
This file is in the _data folder of my Jekyll site
client,opp,person,status
Oracle,"New thing five","Mary Smith",lead
Oracle,"Data plan","Sue Curry",lead
Oracle,"Migration 2019","Sue Curry",lead
IBM, "Platform assessment","Jane Campton",lost
I'd like to display the number of leads per person:
Mary Smith 1 lead
Sue Curry 2 lead
Jane Campion 0 lead
So, for each "person" in "site.opportunities" count the number of rows for each instance of "person" where "status" = "lead"
I now this involves incrementing counters inside nested for loops but I just can't seem to get it working.
I could change the data file format to json or yml but in my current use case I have other non-technical team members editing a csv file in excel so that's a given here.
You can do this with group_by and where filters :
{% assign groups = site.data.opportunities | group_by: "person" %}
<ul>
{% for g in groups %}
{% assign leads = g.items | where: "status", "lead" %}
<li>name : {{ g.name }} {{ leads.size }} leads</li>
{% endfor %}
</ul>

Why converting to JSON a hash I initialized yield a null?

I am trying to build a hash in order to later output is as JSON (and ultimately import it to be reused by a a script). This is part of my static site built with jekyll.
Following the documentation on Expressions and Variables, I created a file with
---
---
{% assign aaa['bbb'] = 'xxx' %}
{{ aaa | jsonify }}
This was compiled by jekyll to a null (as if the hash was not initialized). Why is it so?
Sadly the documentation is talking about reading hash or arrays, not writing.
The only thing you can write from liquid is arrays.
create an empty array : {% assign my-array = "" | split: "/" %}{{
y-array | inspect }}
store with push or shift {% assign my-array = my-array | push: anything %}
= empty-array }}, where anything can be a string, an integer, a hash or an array.

Jekyll get specific _data data based on an name

I have a data folder structure:
_data/footer/1.yml
_data/footer/2.yml etc
What I want to do is within the template, based on the front matter variable is to, select one of those files and return the data contained in them.
If I do this:
site.data.footer.1 it returns the data withn 1.yml. If I try to do site.data.footer.{{page.footer}} it returns nothing, even if the front matter has the footer variable set to 1 like this:
---
footer: 1
---
{% assign foot_id = page.footer %}
{{foot_id}}
{% assign stuff = site.data.footer.{{foot_id}} %}
{{stuff}}
stuff in this case would be blank. Is this the correct way to do this? Whats going wrong?
If we look at your datas :
site.data.footer = {"1"=>{"variable"=>"one"}, "2"=>{"variable"=>"two"}}
we have a hash were keys are strings.
We can access our datas like this :
{{ site.data.footer.1 }} => {"variable"=>"one"}
or
{{ site.data.footer["1"] }} => {"variable"=>"one"}
Note that the bracket notation takes a string as key. If you try with an integer, it returns nothing {{ site.data.footer[1] }} => null.
If we want to use a page variable, we need it to be a string. It can be :
---
# a string in the front matter
footer: "1"
---
{{ site.data.footer[page.footer] }} => {"variable"=>"one"}
or an integer casted to string
---
# a string in the front matter
footer: 1
---
Transform an integer to a string by adding it an empty string
{% assign foot_id = page.footer | append: "" %}
{{ site.data.footer[foot_id] }} => {"variable"=>"one"}
Note: you can also cast a string to an integer like this :
{% assign my_integer = "1" | plus: 0 %}
{{ my_integer | inspect }} => 1 but not "1"

Creating data-structures for ansible templates

I'm trying to create a simple config file that enumerates all the (hostname, ip_address) pairs as part of an ansible task. What I'd really like to do is something like this (using ansible's global datastructures groups and hostvars):
def grouped_hosts():
ret = {}
for group in groups:
ret[group] = {}
for host in groups[group]:
ret[group][host] = hostvars[host]['ansible_eth0']['ipv4']['address']
return json.dumps(ret)
Which would emit a data structure similar to:
{"webservers":{"web0":"1.2.3.4","web1":"1.2.3.5"},{"caches":{"cache0":"1.2.3.6"}}}
However, I don't know how to build and pass this data structure to my jinja2 template. I really want to be able to create that datastructure and just put {{ grouped_hosts()|to_nice_json }} and call it a day. But how do I write, and where do I put, that grouped_hosts() function?
I'm not sure what you're trying to create with your template, but if you just want to output this as a json structure, you can do it this way :
{
{% set gdelim = '' %}
{% for group in groups %}
{{ gdelim }}"{{group}}": {
{% set hdelim = '' %}
{% for host in groups[group] %}
{{ hdelim }}"{{ host }}": "{{hostvars[host]['ansible_eth0']['ipv4']['address']}}"
{% set hdelim = ',' %}
{% endfor %}
}
{% set gdelim = ',' %}
{% endfor %}
}
The gdelim and hdelim are here to set the delimiter when required (note the delimiters prefixing objects).
At first run, the delimiter is empty, and then ",". Since the objects are prefixed by the delimiter, you don't have trailing comas, and thus the resulting JSON is valid (but a bit ugly).

Liquid: Can I get a random element from an Array?

I'm trying to pick a random element from an array -- is this possible using Liquid/Jekyll?
I can create an array -- and access a given index ... but is there a way to "shuffle" the array and then select an index, and thus get a random element from the array?
prefix: ["Foo", "Bar", "Baz"]
---
{{ page.prefix[1] }}
# outputs "Bar"
The 2018 answer is
{% assign prefix = page.prefix | sample: 2 %}
{{ prefix[0] }}
As the OP asked about Jekyll, this can be found at: https://jekyllrb.com/docs/templates/
Liquid doesn't have a filter for picking a random element from an array or an integer interval.
If you want Jekyll to do that, you would have to create an extension to add that liquid filter.
However, I must point out that doing so would pick a random element every time the page is generated, but not every time the page is viewed.
If you want to get different random values every time you visit a page, your best option is using javascript and letting the client pick a random value. You can use liquid to generate the relevant javascript though.
You may be able to do that just in Liquid, but it could less of generic solution like the one provided by #Brendan. According to this article, you can generate a random liquid number between min & max. So simply:
Assign the min to 0 and max to your array's length.
Loop over the array till you find your random number and pick you element.
Here is an example, get your random array index:
{% assign min = 0 %}
{% assign max = prefix.size %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}
Then find your random value:
{{ prefix[randomNumber] }}
You can create a plugin to get a random element. Something like this:
module Jekyll
module RandomFilter
# Use sample to get a random value from an array
#
# input - The Array to sample.
#
# Examples
#
# random([1, 2, 3, 4, 5])
# # => ([2])
#
# Returns a randomly-selected item out of an array.
def random(input)
input.sample(1)
end
end
end
Liquid::Template.register_filter(Jekyll::RandomFilter)
Then do something like this in your template to implement:
{% assign myArray = '1|2|3|4|5 | split: '|' %}
{% assign myNumber = myArray | random %}
Without using a plugin (which might be a requirement if you are using github pages for example) and don't want the choice to be set only at build/rebuild time.
This uses collections as it's data source and some feature flags set in the page front matter.
{% if page.announcements %}
<script>
// homepage callout
var taglines=[
{% for txt in site.announcements %}
{{ txt.content | markdownify | jsonify | append: "," }}
{% endfor %}
]
var selection = document.querySelector('#tagline') !== null;
if(selection) {
document.querySelector('#tagline').innerHTML = taglines[ Math.floor(Math.random()*taglines.length) ];
}
</script>
{% endif %}
I use markdownify to process the content, jsonify to make it JavaScript safe and then append a comma to make my array.
The Javascript then populates one randomly at page load.
Add collection to config.yml
collections:
announcements:
Add flag to page
---
layout: home
title:
slider: true
announcements: true
---
collection content item (test.md)
---
published: true
---
This is a test post
You could adapt Liquid::Drop and whitelist Ruby's sample method.
See https://github.com/Shopify/liquid/blob/master/lib/liquid/drop.rb#L69:
You would need to change:
blacklist -= [:sort, :count, :first, :min, :max, :include?]
to:
blacklist -= [:sort, :count, :first, :min, :max, :include?, :sample]
Next you could just use:
{{ some_liquid_array.sample }}