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?
python/aws_cdk.aws_secretsmanager: JSON.stringify in Python example
How we made JSON.stringify more than twice as fast
Videos
Python has a built-in JSON parsing library. Adding import json provides basic JSON parsing functionality, which can be used as follows:
import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur
person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
More information here: http://docs.python.org/2/library/json.html
Because you are using the wrong variable!
var jsonData = JSON.stringify(jsData);
..
$.ajax({
..,
contentType: "application/json", //Remember to set this.
data: jsData,
^^^^^^ => Shouldn't you be passing "jsonData" here?
When you pass a simple javascript dictionary, jQuery encodes the keys and values in the percentile-encoding format. That is the reason why you are seeing the inner dict as string.
What you have to do ideally (IMO), is pass the JSON string rather than partially percentile encoded string.
Note that you will probably have to change the way your server is reading the data. In this way there would no longer be HTTP/POST request parameters. Just a plain JSON string in the HTTP entity section.