Node.js querystring.stringify() Method

08 Oct, 2021
The querystring.stringify() method is used to produce an URL query string from the given object that contains the key-value pairs. The method iterates through the object’s own properties to generate the query string.
It can serialize a single or an array of strings, numbers, and booleans. Any other types of values are coerced to empty strings.
During serializing, the UTF-8 encoding format is used to encode any character that requires percent-encoding. To encode using an alternative character encoding, the encodeURIComponent option has to be specified.
Syntax:
querystring.stringify( obj[, sep[, eq[, options]]] )
Parameters: This function accepts four parameters as mentioned above and described below:
obj: It is an Object that has to be serialized into the URL query string.
sep: It is a String that specifies the substring used to delimit the key and value pairs in the query string. The default value is “&”.
eq: It is a String that specifies the substring used to delimit keys and values in the query string. The default value is “=”.
options: It is an Object which can be used to modify the behaviour of the method. It has the following parameters:encodeURIComponent: It is a function that would be used to convert URL-unsafe characters to percent-encoding in the query string. The default value is querystring.escape().
encodeURIComponent: It is a function that would be used to convert URL-unsafe characters to percent-encoding in the query string. The default value is querystring.escape().
Return Value: It returns a String that contains the URL query produced from the given object.
Below programs illustrate the querystring.stringify() method in Node.js:
Example 1:
// Import the querystring moduleconst querystring = require("querystring");  // Specify the URL object// to be serializedlet urlObject = {    user: "sam",    access: true,    role: ["admin", "editor", "manager"],};  // Use the stringify() method on the objectlet parsedQuery = querystring.stringify(urlObject);  console.log("Parsed Query:", parsedQuery);
Output:
Parsed Query: user=sam&access=true&role=admin&role=editor&role=manager
Example 2:
// Import the querystring moduleconst querystring = require("querystring");  // Specify the URL object// to be serializedlet urlObject = {    user: "max",    access: false,    role: ["editor", "manager"],};  // Use the stringify() method on the object// with sep as `, ` and eq as `:`let parsedQuery = querystring.stringify(urlObject, ", ", ":");  console.log("Parsed Query 1:", parsedQuery);  // Use the stringify() method on the object// with sep as `&&&` and eq as `==`parsedQuery = querystring.stringify(urlObject, "&&&", "==");  console.log("\nParsed Query 2:", parsedQuery);
Output:
Parsed Query 1: user:max, access:false, role:editor, role:manager

Parsed Query 2: user==max&&&access==false&&&role==editor&&&role==manager
Reference: https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options
Node.js- querystring-Module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React