If I have a list as follows:
[
{
"a": "1",
"b": "2"
},
{
"a": "3",
"b": "4"
},
{
"a": "5",
"b": "6"
}
]
How can I rename the keys of the first list and append static items to each dict using a Jijja2 filter?
For example:
[
{
"a2": "1",
"b2": "2",
"c2": "test"
},
{
"a2": "3",
"b2": "4",
"c2": "test"
},
{
"a2": "5",
"b2": "6"
"c2": "test"
}
]
I have attempted something like the following:
{{ my_list | map('json_query', '{a2:a, b2:b}') | list }}
Which is half the battle, but how would I append a static item to the resulting dict?
The following should do:
- debug:
msg: >-
{{ my_list | json_query("[].{a2: a, b2: b, c2: 'test' }") }}
Result with a copy paste of your data:
"msg": [
{
"a2": "1",
"b2": "2",
"c2": "test"
},
{
"a2": "3",
"b2": "4",
"c2": "test"
},
{
"a2": "5",
"b2": "6",
"c2": "test"
}
]
}
The order of single and double quotes nesting matters (See jmespath specification). If you put the single/double quotes the other way arround, the result of your static string will be "None"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments