Related
My playbook task is to fetch all resource groups then list all the vms under each resource group and for each vm find the tags associated with it. I am getting correct output when running my playbook. But I am not able to save it correctly using jinja template. Here, is the template I am using:
{
{% for rg in group_list %}
"resource_group": "{{ rg }}",
{% for vm in vm_list %}
"vmId": "{{ vm }}",
{% if vmTag_json|length > 0 %}
{% for key, value in vmTag_json.items() %}
"Key": "{{ key }}",
"Value": "{{ value }}"
{% if not loop.last %},{% endif %}
{% endfor %}
{% else %}
"tags": "NA"
{% endif %}
{% endfor %}
{% endfor %}
}
And the output I am getting is:
{
"resource_group": "RG1",
"vmId": "VM-01",
"Key": "Test",
"Value": "win"
"vmId": "VM-02",
"Key": "Test",
"Value": "win"
}
Tags value is coming same for every machine which is the last value in the loop. Also a "," should come after "Value": "win" if the loop is not ended, it is also not coming and if I put it then it also comes after the last value.
Im trying to use the twig |sort function but find it rather confusing to figure out how to sort with numbers ranging lowest to highest.
Documentation: https://twig.symfony.com/doc/3.x/filters/sort.html
My code example:
{% for duel in group %}
{% duel.intvalue %}
{% endfor %}
Where intvalue is the data needing sorting
Can anyone help me out
I think the documentation is clear enough.
let say you have array of object users
{
"users" : [
{
"name": "third",
"age": 29
},
{
"name": "first",
"age": 2
},
{
"name": "first",
"age": 2
},
{
"name": "second",
"age": 28
}
]
}
sort your data by name
{% for user in users|sort %}
{{ user.name }},
{% endfor %}
sort your data by age
{% for user in users|sort %}
{{ user.age }},
{% endfor %}
sort data by age and show its name
{% for user in users|sort((a, b) => a.age <=> b.age) %}
{{ user.name }},
{% endfor %}
https://twigfiddle.com/tca0vz
A working example would be:
{# setup the expample #}
{% set group = [{intvalue:5},{intvalue:1},{intvalue:4},{intvalue:3},{intvalue:2}] %}
{% for duel in group|sort((a, b) => a.intvalue <=> b.intvalue) %}
{{ duel.intvalue }}
{% endfor %}
Or see this fiddle: https://twigfiddle.com/ru1gpd
I want to remove the comma from the last object in nested for loop:
[{% for item1 in articles.results.entities %}
{% for item2 in articles1.results.entities %}
{% if item1.knowledgearticleid != item2.knowledgearticleid %}
{
"Id":"{{item1.knowledgearticleid}}",
"Title":"{{item1.title}}",
"Articlepublicumber":"{{item1.articlepublicnumber}}",
"Description":"{{item1.description}}"
}
{% endif %}
{% endfor %}
{% unless forloop.last %},{% endunless %}
{% endfor %}]
But this is not working; it is giving me duplicate commas:
[
{
"Id": "ddcb41c6-1f33-ea11-a813-000d3a3be5cf",
"Title": "1 Test1",
"Articlepublicumber": "KA-01992",
"Description": "Test1"
},,
{
"Id": "9564dc21-9df6-414b-ab99-da4ba534fd83",
"Title": "Test2",
"Articlepublicumber": "KA-03363",
"Description": "Test2"
}
]
Identical issue in terms of Shopify liquid snippet discussed here and this is common scenario with concatenation in any language.
With each accepted iteration, add a comma and then the “needed item”. Except if this is the first accepted iteration, don't add the comma.
I just manage to modify your code, but test it on your end. But you got the idea right?
[{% assign list_items = "" %}
{% for item1 in articles.results.entities %}
{% for item2 in articles1.results.entities %}
{% if item1.knowledgearticleid != item2.knowledgearticleid %}
{% unless list_items == "" %}
{% assign list_items = "zzz" %}
{
"Id":"{{item1.knowledgearticleid}}",
"Title":"{{item1.title}}",
"Articlepublicumber":"{{item1.articlepublicnumber}}",
"Description":"{{item1.description}}"
}
{% endunless %}
{% unless list_items == "zzz" %}
,{
"Id":"{{item1.knowledgearticleid}}",
"Title":"{{item1.title}}",
"Articlepublicumber":"{{item1.articlepublicnumber}}",
"Description":"{{item1.description}}"
}
{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}]
I am using the Transform XML to JSON.
This is my request body in XML
<Root>
<test>
<column1>value1</column1>
<column2>value2</column2>
</test>
<test>
<column1>value1</column1>
<column2>value2</column2>
</test>
</Root>
and this is my liquid map
{
"test": [
{% for data in Root.test %}
{
"column1": "{{data.column1}}",
"column2": "{{data.column2}}",
}
{% endfor %}
]
}
while running the logic app i am getting parsing error:
{
"Code": "IncorrectLiquidTransformOutputType",
"Message": "An error occurred while converting the transformed value to JSON. The transformed value is not a valid JSON. 'After parsing a value an unexpected character was encountered: {. Path 'test[0]'"
}
But while I run the same code in the liquid sandbox is working fine. may I know? what is the issue here with logic apps.
I test it in my side and provide my liquid map below for your reference:
{
"test": [
{% for data in content.Root %}
{
"column1": "{{data.column1}}",
"column2": "{{data.column2}}"
},
{% endfor %}
]
}
Run this liquid map and it works fine(shown as below screenshot)
Hope it helps~
I'll summarise the answers/comments already provided as I didn't see the issue when I first checked the accepted answer!
This type of error can be caused by missing out a comma at the end of a JSON object literal that you want to be repeated as elements in the array, in the transformed output.
Fails
[
{% for data in Root.test %}
{
"column1": "{{data.column1}}",
"column2": "{{data.column2}}",
}
{% endfor %}
]
Works
[
{% for data in Root.test %}
{
"column1": "{{data.column1}}",
"column2": "{{data.column2}}",
},
{% endfor %}
]
You should use if condition to add "," {% if forloop.last == false %},{% endif %}
{
"test": [
{% for data in content.Root %}
{
"column1": "{{data.column1}}",
"column2": "{{data.column2}}"
}{% if forloop.last == false %},{% endif %}
{% endfor %}
]}
I need to order a list of objects i receive with an inner property.
I receive a list of objects like that :
{ match: "italy - germany", date: "27/01/2019", competion: "World cup" }
{ match: "lille - paris", date: "23/01/2019", competion: "coupe de france" }
{ match: "om - psg", date: "13/01/2019", competion: "coupe de france" }
{ match: "russia - poland", date: "25/01/2019", competion: "World cup" }
I don't know where to start but i need to loop over matches:
{% for match in matches %}
I want to obtain this list:
coupe de france :
om - psg
lille -paris
world cup :
italy germany
russia - poland
{% set data = [
{
'country' : 'france',
'match': 'G',
},
{
'country' : 'belgium',
'match': 'F',
},
{
'country' : 'france',
'match': 'E',
},
{
'country' : 'germany',
'match': 'D',
},
{
'country' : 'germany',
'match': 'C',
},
{
'country' : 'france',
'match': 'B',
},
{
'country' : 'italy',
'match': 'A',
},
] %}
{% set sorted = [] %}
{% for row in data %}
{% if not ((row.country) in sorted|keys) %}{% set sorted = sorted|merge({ (row.country) : [], }) %}{% endif %}
{% set sorted = sorted|merge({(row.country): (sorted[(row.country)]|merge([ row.match, ])|sort)}) %}
{% endfor %}
{% for country, values in sorted %}
{{ country }}
{% for value in values %}
- {{ value }}
{% endfor %}
----------------------------
{% endfor %}
demo
You can try snilius/twig-sort-by-field.
With this Twig extension, you can do somthing like this:
{% for match in matchs|sortbyfield('competion', 'desc') %}
{{ dump(match) }}
{% endfor %}