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=(',', ':'))
Answer from Mike Cluck on Stack OverflowLong story short, is I have a wrapper over an API that pulls in a JSON from a rest call. I use json.loads() to return the response in my Python wrapper. I then use the response to make a REST call using POST method.
My problem is that I'm getting true, false, and none values converted to True, False, and None. When I make my second REST call I need them to be in the form of 'true', 'false, and 'none'. Basically, I want to preserve my string, float, and integers while converted these other types to strings.
Any ideas?
The equivalent to Python's json.dumps() in JavaScript is JSON.stringify() as in:
var jsonstr = JSON.stringify(someVariable);
Valid JSON doesn't contain structures like u'something', only "something". If you really have a string like that, it's likely from Python via repr() or similar.
If you're trying to convert Python objects to JavaScript objects from within their respective environments, in Python you would convert them to a JSON encoded strings using json.dumps(), transfer the strings to the JavaScript environment, and then use JSON.parse() to convert them back into objects.
(Keep in mind that JSON doesn't understand anything beyond some basic types such as string, float, boolean, array, and key:value structures. For example, trying to transfer a Python datetime object is likely to get you string or a collection key:value pairs rather than an actual JavaScript Date object.)
The difference is that json.dumps applies some minor pretty-printing by default but JSON.stringify does not, you can see below for same.
Python:
>>> import json
>>> json.dumps({"candidate" : 5, "data": 1})
'{"candidate": 5, "data": 1}'
Javacript:
> JSON.stringify({"candidate" : 5, "data": 1})
'{"candidate":5,"data":1}'
But with some modification, we can have the same JSON string, and to verify both are the same JSON string in formatting as well, we can generate the hash for both JSON strings and compare. There are two ways for it:-
- Modifying
javascript JSON stringto make it equivalent to apython JSON string.
Python:
Javacript:>>> import json,hashlib >>> a = json.dumps({"candidate" : 5, "data": 1}, sort_keys=True) >>> hashlib.md5(a.encode("utf-8")).hexdigest() '12db79ee4a76db2f4fc48624140adc7e'> const Crypto = require("crypto-js") undefined > const a = JSON.stringify({"candidate" : 5, "data": 1}).replaceAll(":", ": ").replaceAll(",", ", ") undefined > Crypto.MD5(a).toString(Crypto.enc.Hex) '12db79ee4a76db2f4fc48624140adc7e' - Modifying
python JSON stringto make it equivalent to ajavascript JSON string.
Python:
Javacript:>>> import json,hashlib >>> a = json.dumps({"candidate" : 5, "data": 1}, separators=(',', ':')) >>> hashlib.md5(a.encode("utf-8")).hexdigest() '92e99f0a99ad2a3b5e02f717a2fb83c2'> const Crypto = require("crypto-js") undefined > const a = JSON.stringify({"candidate" : 5, "data": 1}) undefined > Crypto.MD5(a).toString(Crypto.enc.Hex) '92e99f0a99ad2a3b5e02f717a2fb83c2'Note:- To run javascript code, crypto-js npm pkg should be installed as same location where you started the node shell.