0

I have a python code where in routes.py I have the following render:

    return render_template(
        "test.html",
        stockdatalist={"TATACONSUM": '{"price":"123"}'}
    )

Now in my HTML, if I do:

{% set stockdata = stockdatalist['TATACONSUM'] %}
<p> {{ stockdata }} </p>

it prints:

{"price":"123"}

However, if I change it to <p> {{ stockdata.price }} </p>, it stops working and prints blank.

I could use some help! Much appreciated.

I tried using json.dumps and json.loads in routes.py as well using filters like |tojson in HTML, but none of them worked.

Pratik Ray
  • 19
  • 3
  • 2
    JUst remove those single quotes from there if you literally have that value? `'{"price":"123"}'` should be `{"price":"123"}`? – Abdul Aziz Barkat Aug 23 '23 at 04:35
  • That's the thing. I'm not defining the JSON, I'm just receiving it from another Webservice. I'm not able to figure out how to remove the outer single quotes as there may be other single quotes in the rest of the JSON. – Pratik Ray Aug 23 '23 at 13:38
  • Does this answer your question? [How can I parse (read) and use JSON in Python?](https://stackoverflow.com/questions/7771011/how-can-i-parse-read-and-use-json-in-python) You say you tried using `json.loads` well that **is** the solution, from your example: `stockdatalist={"TATACONSUM": json.loads('{"price":"123"}')}` – Abdul Aziz Barkat Aug 23 '23 at 14:04
  • Thank you. I tried updating the entire json with json.loads which didn't work. Please post as answer so I can accept it – Pratik Ray Aug 23 '23 at 14:38
  • "_entire JSON_" the part which I called `json.loads` on is the only JSON string in your example code, the rest is a python dictionary...This question is a duplicate since you are asking about parsing JSON. – Abdul Aziz Barkat Aug 23 '23 at 14:41

1 Answers1

0

stockdatalist={"TATACONSUM": '{"price":"123"}'}

are you defining a string value to "TATACONSUM" key inn that dict? Because then when you access that key its a string not a dictionary