O
O
Olya Grigorenko2017-09-29 10:19:13
JSON
Olya Grigorenko, 2017-09-29 10:19:13

How to work with json in Python?

There is a table of users, in it the standard data on users.

The likes column contains json likes in the format [{"likes":"one"},{"likes":"two"}].
When receiving information about a user, data with likes is sent to the template in string format.

You need to present the data in a formatted form. so this line doesn't work.

How to work with data in this case?

Now I'm thinking about writing a separate request for getting characteristics (likes), transferring the received to json (dict as an option) on the controller and giving it to the template separately?

Maybe there are other options?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
longclaps, 2017-09-29
@longclaps

import json

d = json.loads('[{"likes":"one"},{"likes":"two"}]')
print(d[0]["likes"])

D
Dmitry, 2017-09-29
@dmtrrr

Never store json as a string in a relational database.

S
sim3x, 2017-09-29
@sim3x

https://dev.mysql.com/doc/refman/5.7/en/json-searc...
In the controller / view, receive and process the response and push it into the context - the template must be dumb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question