Asdfg
Asdfg

Reputation: 12213

MongoDB - Return list with items surrounded with double quotes

I am querying MongoDB collection using pymongo which works fine except that the return objects are surrounded by single quotes.

Is there some setting that I can pass so that the returned objects are surrounded with double quotes?

This is how I am querying the collection:

with MongoClient('localhost', 27017) as client:
    db = client['somedb']
    collection = db['somecollection']
    return list(collection.find())

This is the output:

<class 'list'>: ['a', 'b', 'c']

Desired output:

<class 'list'>: ["a", "b", "c"]

This is how I am assigning data attribute:

<div class="card" data-mylist="{{ list_of_data }}">

This is how I am accessing data attribute:

data = $(this).data( "mylist" )

This is how element looks like when I inspect it:

Upvotes: 0

Views: 500

Answers (1)

oste-popp
oste-popp

Reputation: 190

Flask has some standard-filters to handle python data in jinja. http://flask.pocoo.org/docs/1.0/templating/#standard-filters

tojson is one of them. It converts a python object to JSON.

It can be used by piping the data inside the jinja delimiter.

<div class="card" data-mylist="{{ list_of_data | tojson }}">

Upvotes: 2

Related Questions