Python Transformation None for JavaScript null

In a Django view, I create a dataset something like this:

data = [22, 23, 18, 19, 21, None, 22, 20] 

I pass this data to a JavaScript variable using:

 data_json = simplejson.dumps(data) 

For use in High Charts script.

Unfortunately, JavaScript is encountered when it encounters a value of None , because in fact I need null . How can I best replace None with null , and where do I need it - in a Django view or in JavaScript?

+6
source share
1 answer

If you are using Python 2.6 or later, you can use the json built-in module:

 >>> import json >>> json.dumps([1, 2, 3, None, 4]) '[1, 2, 3, null, 4]' 

See http://docs.python.org/library/json.html

+19
source

Source: https://habr.com/ru/post/912067/


All Articles