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
🌐
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.
🌐
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 22, 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?

🌐
ReqBin
reqbin.com › code › javascript › wqoreoyp › javascript-json-stringify-example
How to stringify a JavaScript object to JSON string?
The JSON.stringify(value, replacer, space) method converts JavaScript objects to a JSON string. The resulting JSON string is a JSON-formatted or serialized object that can be sent over the network or stored on a disk.
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
If True (the default), their JavaScript equivalents (NaN, Infinity, -Infinity) are used. cls (a JSONEncoder subclass) – If set, a custom JSON encoder with the default() method overridden, for serializing into custom datatypes. If None (the default), JSONEncoder is used. indent (int | str | None) – If a positive integer or string, JSON array elements and object members will be pretty-printed with that indent level.
🌐
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().
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.

🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.
🌐
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.
Find elsewhere
🌐
JSONLint
jsonlint.com › json-stringify
JSON Stringify - Escape JSON for Embedding | JSONLint | JSONLint
If you have a JavaScript object and want JSON, use JSON.stringify(obj).
🌐
W3Schools
w3schools.com › jsref › jsref_stringify.asp
JavaScript JSON stringify() Method
The JSON.stringify() method converts JavaScript objects into strings. When sending data to a web server the data has to be a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript | MDN
The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
🌐
freeCodeCamp
freecodecamp.org › news › json-stringify-example-how-to-parse-a-json-object-with-javascript
JSON Stringify Example – How to Parse a JSON Object with JS
January 5, 2021 - Luckily, this works the same way as in the browser – just use JSON.stringify() to convert JavaScript object literals or arrays into a JSON string:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-json-stringify-an-array-of-objects-in-javascript
How to JSON Stringify an Array of Objects in JavaScript ? - GeeksforGeeks
August 5, 2025 - Example: The below example uses JSON.stringify with a Replacer Function to JSON stringify an array of objects in JavaScript. ... const data = [ { language: 'JavaScript', category: 'Web Development' }, { language: 'Python', category: 'Data Science' }, { language: 'Java', category: 'Software Development' } ]; const res = JSON.stringify(data, (key, value) => { if (typeof value === 'string') { return value.toUpperCase(); } return value; }); console.log(res);
🌐
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....
🌐
CodingNomads
codingnomads.com › javascript-json-stringify-parse
How to Work With JSON in JavaScript
This code snippet demonstrates how a JavaScript object is transformed into a JSON string with the JSON.stringify() method.
🌐
Jsontostring
jsontostring.com
Convert JSON to String Online
Convert JSON to String Online with our tool. Our Javascript Object to String Converter can convert multiline content with full accuracy online.
🌐
ReqBin
reqbin.com › code › javascript › ounkkzpp › javascript-pretty-print-json-example
How do I pretty print JSON in JavaScript?
const str = '{"Id":78912,"Customer":"Jason Sweet", "Quantity":1,"Price":18.00}'; const obj = JSON.parse(str); console.log(JSON.stringify(obj, null, 2)); // output: // { // "Id": 78912, // "Customer": "Jason Sweet", // "Quantity": 1, // "Price": 18 // } ... How do I send a POST request using JavaScript? How to send Bearer Token with JavaScript Fetch API? How do I fetch JSON using JavaScript Fetch API? How to dump Python object to JSON using json.dumps()?
🌐
Hacker News
news.ycombinator.com › item
TIL, thanks. But uh, I expected `JSON.stringify` to throw errors when met with u... | Hacker News
December 26, 2023 - I also think the automatic conversion of a `Date` to a string is a bad thing, which again is prohibited in Python · But I think in the case of JSON.stringify it’s more about use case. 99% of the time, users of this method are taking some data and serialising it to a JSON compliant form to ...