How do I append static items to a dict using Jinja2

pkiller162

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?

Zeitounator

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I include a HTML file in a Jinja2 template?

How do I format a date in Jinja2?

Get nested dict items using Jinja2 in Flask

How do you display markdown value using jinja2?

how do I set a list item by index in jinja2

Ansible/Jinja2 how to append key into list of dict

How do I assign a jinja2 variable value to use later in template?

how to append to a list in jinja2 for ansible

How do I use a global variable in Jinja2 using namespace class?

How do I loop over rows and columns of a pandas dataframe in jinja2?

How do I access specific elements in JSON through Jinja2

How do I filter only letters in jinja2

How do I map list of strings into a list of objects in Ansible/Jinja2?

how can i do a feed of posts with flask and Jinja2?

Append cart, how do I discount certain items?

If jinja markup is in string format, how do I use it in django and jinja2?

How do I eliminate extra whitespace between macro calls in Ansible Jinja2 templates?

How do I append items to an Observable

How do I view a session variable using Flask/Jinja2?

How do I print a dictionary on a single line in jinja2?

I can easily append dictionary items but how do I append class items?

How do I remove duplicated template inheritance in Mobile and Desktop with Jinja2?

How do I show link image by using Python Flask jinja2?

How to get dict key value format using Jinja2 in ansible using debug or set fact module

How do I fetch a value from a var file using Jinja2 into an JSON file in Ansible

ansible - create list of dict using jinja2

Jinja2 error - [ 'zip' is undefined] How do I iterate through 2 lists at once in Jinja 2?

How can I pass pure sql into a macro and then do the iteration in a dbt jinja2 macro

Append dict items to a dict in Python

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive