I have data that is represented in this manner:
{
"key1": [{
"name": "some name1",
"index": "some idx1"
},
{
"name": "some name2",
"index": "some idx2"
},
{
"name": "some name3",
"index": "some idx3"
}],
"key2": [{
"name": "some name4",
"index": "some idx4"
},
{
"name": "some name5",
"index": "some idx5"
},
{
"name": "some name6",
"index": "some idx6"
}]
}
I would like to convert the above to this, which is basically a dictionary with the existing key to a list of the indices.
{
"key1": [some idx1, some idx2, some idx3],
"key2": [some idx4, some idx5, some idx6]
}
I have seen a couple of examples using map, extract and combine, but couldn't quite get it to work as yet. I was however able to do it using jinja, the code is below.
My question is, what is the best way to accomplish the above. What is the recommended best practice with regards to this kind of things - is there any reason why more complex operations shouldn't be done using jinja2 (given that the one liners i have seen are overly complex and probably would be difficult for others to figure out - consequently making the script difficult to maintain).
here is the code that does the trick, but again, not sure if the best way of accomplishing this:
- hosts: local
tags: test1
gather_facts: False
vars:
dict1:
key1:
- { name: some name1, index: some idx1 }
- { name: some name2, index: some idx2 }
- { name: some name3, index: some idx3 }
key2:
- { name: some name4, index: some idx4 }
- { name: some name5, index: some idx5 }
- { name: some name6, index: some idx6 }
tasks:
- name: "dict of list of dict"
set_fact:
index_map: |
{% set map = dict() %}
{% for k,v in dict1.iteritems() %}
{% set x=map.__setitem__(k, []) %}
{% for item in v %}
{% set x= map[k].append(item.name) %}
{% endfor %}
{% endfor %}
{{ map }}
- debug:
msg: "{{ index_map }}"
To expand on the problem im trying to solve a bit more: Given an 'index' , i want to find the key it is associated it with. The target structure would allow me to do that a bit easier i think. so either a dict of key to a list of index, or a dict of index to key would suffice.
Thanks for any advice..
See Question&Answers more detail:
os