Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is my first attempt at using Jinja or anything Python related, but our vendor has a new tool implemented that uses it, so here we are.

As a test, I'm using a free API as a data source and want to convert it through the tool's Jinja. API is http://www.floatrates.com/daily/usd.json...

{"eur":{
    "code":"EUR",
    "alphaCode":"EUR",
    "numericCode":"978",
    "name":"Euro",
    "rate":0.90140326265491,
    "date":"Tue, 21 Jan 2020 12:00:01 GMT",
    "inverseRate":1.1093813850359},
"gbp":{
    "code":"GBP",
    "alphaCode":"GBP",
    "numericCode":"826",
    "name":"U.K. Pound Sterling",
    "rate":0.76735727130961,
    "date":"Tue, 21 Jan 2020 12:00:01 GMT",
    "inverseRate":1.3031739417721},
"jpy":{
    "code":"JPY",
    "alphaCode":"JPY",
    "numericCode":"392",
    "name":"Japanese Yen",
    "rate":109.99188913429,
    "date":"Tue, 21 Jan 2020 12:00:01 GMT",
    "inverseRate":0.009091579459819}...

My goal is to iterated through the content (which this tool has the response's body in the content property) and grab the code and eventual rate for each one. I know what to do once I can successfully get those values... Here is what I have so far

{% set Collection = a.content|to_json %}
{% for obj in Collection %}
{% set CurrentObject = obj %}
{{ Collection[CurrentObject].code }}
{% endfor %}

This doesn't appear to work however... if I do a.content|to_json['eur'].code then I get the EUR as a value, but I'm trying to not hardcode for each iteration.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
885 views
Welcome To Ask or Share your Answers For Others

1 Answer

I don't think you want that to_json filter there. That's for producing a JSON representation of a data structure. You don't want that; you actually want the data structure.

If you have the data in your question in the content property, you could write something like this:

{% for code in content %}
{{ code }} {{ content[code].rate }}
{% endfor %}

This would give you:

eur 0.90140326265491

gbp 0.76735727130961

jpy 109.99188913429

This is the code I'm using to test:

import jinja2


content = {
    "eur": {
        "code": "EUR",
        "alphaCode": "EUR",
        "numericCode": "978",
        "name": "Euro",
        "rate": 0.90140326265491,
        "date": "Tue, 21 Jan 2020 12:00:01 GMT",
        "inverseRate": 1.1093813850359,
    },
    "gbp": {
        "code": "GBP",
        "alphaCode": "GBP",
        "numericCode": "826",
        "name": "U.K. Pound Sterling",
        "rate": 0.76735727130961,
        "date": "Tue, 21 Jan 2020 12:00:01 GMT",
        "inverseRate": 1.3031739417721,
    },
    "jpy": {
        "code": "JPY",
        "alphaCode": "JPY",
        "numericCode": "392",
        "name": "Japanese Yen",
        "rate": 109.99188913429,
        "date": "Tue, 21 Jan 2020 12:00:01 GMT",
        "inverseRate": 0.009091579459819,
    },
}


t = jinja2.Template('''{% for code in content %}
{{ code }} {{ content[code].rate }}
{% endfor %}''')

print(t.render(content=content))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...