JSON.stringify (Javascript) and json.dumps (Python) not equivalent on a list?

JavascriptPythonJson

Javascript Problem Overview


In javascript:

var myarray = [2, 3];
var json_myarray = JSON.stringify(myarray) // '[2,3]'

But in Python:

mylist = [2, 3]
json_mylist = json.dumps(mylist) # '[2, 3]' <-- Note the space

So the 2 functions aren't equivalent. It's a bit unexpected for me and a bit problematic when trying to compare some data for example.

Some explanation about it?

Javascript Solutions


Solution 1 - Javascript

The difference is that json.dumps applies some minor pretty-printing by default but JSON.stringify does not.

To remove all whitespace, like JSON.stringify, you need to specify the separators.

json_mylist = json.dumps(mylist, separators=(',', ':'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThePhiView Question on Stackoverflow
Solution 1 - JavascriptMike CluckView Answer on Stackoverflow