1

I want to insert data from json-format into a webapp through flask to put values into html:

spec_player.html:

{% for p in posts: %}
    <p>{{p.first_name}}</p>
{% endfor %}
  1. This works (main.py):
posts = [
    {
        "id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",
        "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250
    }
]

@app.route("/spec")
def spec_player():
    return render_template("spec_player.html", posts=posts)
  1. This doesn't work (main.py):
posts = [
    {
        "data":[{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",
        "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}],
        "meta":{"total_pages":1,"current_page":1,"next_page":null,"per_page":25,"total_count":1}
    }
]

@app.route("/spec")
def spec_player():
    return render_template("spec_player.html", posts=posts)

I wonder if there is a way to get the 2. json-format into the 1. format? (I only get the 2. json-format from an api) OR writing an other query inside html (something like p.data.first_name)?

2 Answers 2

1

If you always retrieve input data in second format you can transform it to first format like this:

import itertools
flatten = itertools.chain.from_iterable

def transform(posts):
    transformed = list(map(lambda post: post["data"], posts))
    flat_posts = list(flatten(transformed))
    return flat_posts

example:

posts = [
    {
        "data":[{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",
        "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}],
        "meta":{"total_pages":1,"current_page":1,"next_page":None,"per_page":25,"total_count":1}
    }
]

print(transform(posts))

>>> [
  {
    'id': 237, 'first_name': 'LeBron', 'height_feet': 6, 'height_inches': 8, 'last_name': 'James', 'position': 'F', 
    'team': {'id': 14, 'abbreviation': 'LAL', 'city': 'Los Angeles', 'conference': 'West', 'division': 'Pacific', 'full_name': 'Los Angeles Lakers', 'name': 'Lakers'}, 'weight_pounds': 250
  }
]

Sign up to request clarification or add additional context in comments.

4 Comments

This works pretty well. Is it possible to keep the [ ] in which the data is inside? I need them to access the variables.
@fid I'm not sure I understand what you asking for, could you please paste somewhere example what you want to achieve?
I need the form like this: [{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F", "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}] So the squared brackets should stay.
Looks like it's the same format as format-1 in your post. If you need without flatten, like this: [[{'id': 237, 'first_name': 'LeBron', 'height_feet': 6, 'height_inches': 8, 'last_name': 'James', 'position': 'F', 'team': {'id': 14, 'abbreviation': 'LAL', 'city': 'Los Angeles', 'conference': 'West', 'division': 'Pacific', 'full_name': 'Los Angeles Lakers', 'name': 'Lakers'}, 'weight_pounds': 250}]] , then just remove flatten call (in transform function)
1

What you are looking for is filter and flatten the 2nd posts JSON before passing into rendering the template. For example, you could do;

def fatten(json):
    flatten_json = []
    for node in json:
        d = node["data"]
        if d is not None:
            for item in d:
                flatten_json.append(item)
    return flatten_json

Or more Pythonic (but not so readable) version

def flatten(json):
    return [item for node in json if node["data"] is not None for item in node["data"]]

Then pass the flattened json as

return render_template("spec_player.html", posts=fatten(posts))

Both functions iterate over posts JSON and extract child nodes in each data node.

I don't think pulling a library for this simple task is worth it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.