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 Overflow
🌐
Reddit
reddit.com › r/learnpython › is there a python equivivalent of javascript's json.stringify() ?
r/learnpython on Reddit: Is there a Python equivivalent of JavaScript's JSON.stringify() ?
February 24, 2015 -

Long 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
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
It should return a JSON encodable version of the object or raise a TypeError. If None (the default), TypeError is raised. sort_keys (bool) – If True, dictionaries will be outputted sorted by key. Default False. Changed in version 3.2: Allow strings for indent in addition to integers.
🌐
Delft Stack
delftstack.com › home › howto › python › python json stringify
How to Stringify JSON in Python | Delft Stack
February 2, 2024 - To make this happen, a process called serialization takes place, which is like turning complicated data into a simple text form. In the programming languages JavaScript and Python, there are specific functions that handle this serialization task. In JavaScript, there’s a function called JSON.stringify(), and in Python, it’s json.dumps().
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
Convert a Python object containing all the legal data types: import json x = { "name": "John", "age": 30, "married": True, "divorced": False, "children": ("Ann","Billy"), "pets": None, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } print(json.dumps(x)) Try it Yourself » · The example above prints a JSON string, but it is not very easy to read, with no indentations and line breaks.
🌐
freeCodeCamp
freecodecamp.org › news › python-json-how-to-convert-a-string-to-json
Python JSON – How to Convert a String to JSON
November 9, 2021 - The json.loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-string-to-json-object
Convert String to JSON Object - Python - GeeksforGeeks
json.loads() method is the most commonly used function for parsing a JSON string and converting it into a Python dictionary. The method takes a string containing JSON data and deserializes it into a Python object.
Published   July 11, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-json-to-string
Convert JSON to string - Python - GeeksforGeeks
July 12, 2025 - This code makes a GET request to a dummy API to fetch employee data, converts the JSON response into a Python dictionary using json.loads(), and then converts it back into a JSON string using json.dumps().
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › convert json object to string in python
Convert JSON Object to String in Python - Spark By {Examples}
May 21, 2024 - We are often required to convert ... for example, json.dumps() is utilized to convert JSON to string and also we can directly use the str() and repr() methods that will convert json object to a string....
🌐
ReqBin
reqbin.com › code › python › g4nr6w3u › python-parse-json-example
How to parse a JSON with Python?
Using the JSON Encoder and Decoder module, you can serialize (dump) Python objects to JSON strings or deserialize (parse) JSON strings to Python objects. The JSON encoder can create pretty or compact JSON strings, or convert Python objects directly ...
🌐
DEV Community
dev.to › kiani0x01 › python-json-stringify-guide-ea5
Python JSON Stringify Guide - DEV Community
July 30, 2025 - Learn how to convert Python objects into JSON strings reliably using json.dumps, custom encoders, formatting options, and performance tips. Tagged with python, json, stringify, serialization.
Top answer
1 of 2
20

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.)

2 of 2
4

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:-
  1. Modifying javascript JSON string to make it equivalent to a python JSON string.
    Python:
    >>> import json,hashlib
    >>> a = json.dumps({"candidate" : 5, "data": 1}, sort_keys=True)
    >>> hashlib.md5(a.encode("utf-8")).hexdigest()
        '12db79ee4a76db2f4fc48624140adc7e'
    
    Javacript:
    > 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'
    
  2. Modifying python JSON string to make it equivalent to a javascript JSON string.
    Python:
    >>> import json,hashlib
    >>> a = json.dumps({"candidate" : 5, "data": 1}, separators=(',', ':'))
    >>> hashlib.md5(a.encode("utf-8")).hexdigest()
        '92e99f0a99ad2a3b5e02f717a2fb83c2'
    
    Javacript:
    > 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.

Top answer
1 of 2
201

json.dumps() is much more than just making a string out of a Python object, it would always produce a valid JSON string (assuming everything inside the object is serializable) following the Type Conversion Table.

For instance, if one of the values is None, the str() would produce an invalid JSON which cannot be loaded:

>>> data = {'jsonKey': None}
>>> str(data)
"{'jsonKey': None}"
>>> json.loads(str(data))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

But the dumps() would convert None into null making a valid JSON string that can be loaded:

>>> import json
>>> data = {'jsonKey': None}
>>> json.dumps(data)
'{"jsonKey": null}'
>>> json.loads(json.dumps(data))
{u'jsonKey': None}
2 of 2
2

There are other differences. For instance, {'time': datetime.now()} cannot be serialized to JSON, but can be converted to string. You should use one of these tools depending on the purpose (i.e. will the result later be decoded).

🌐
Python Examples
pythonexamples.org › convert-python-class-object-to-json
Python - Convert Class Object to JSON
Python - Convert Object to JSON - To convert a Python Class Object to JSON String, or save the parameters of the class object to a JSON String, use json.dumps() method.
🌐
Milddev
milddev.com › python-json-stringify-guide
Python JSON Stringify Guide
While many developers focus on parsing JSON, converting Python objects into JSON strings often gets less attention. This stringification process has nuances—like handling custom types or ensuring readable output—that can trip you up in real projects. Have you ever wondered how to serialize ...
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JSON.stringify() can not only convert objects and arrays into JSON strings, it can convert any JavaScript value into a string. ... In JSON, date objects are not allowed.
🌐
GitHub
github.com › aws › aws-cdk › issues › 24931
python/aws_cdk.aws_secretsmanager: JSON.stringify in Python example · Issue #24931 · aws/aws-cdk
April 4, 2023 - # Templated secret with username and password fields templated_secret = secretsmanager.Secret(self, "TemplatedSecret", generate_secret_string=secretsmanager.SecretStringGenerator( secret_string_template=JSON.stringify({"username": "postgres"}), generate_string_key="password" ) ) https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_secretsmanager/README.html ... @aws-cdk/aws-secretsmanagerRelated to AWS Secrets ManagerRelated to AWS Secrets ManagerbugThis issue is a bug.This issue is a bug.documentationThis is a problem with documentation.This is a problem with documentation.effort/smallSmall work item – less than a day of effortSmall work item – less than a day of effortp2
Author   petertaylor3
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - Learn how to work with JSON data in Python using the json module. Convert, read, write, and validate JSON files and handle JSON data for APIs and storage.