The Wayback Machine - https://web.archive.org/web/20241118031236/https://www.geeksforgeeks.org/node-js-querystring-stringify-method/
Open In App

Node.js querystring.stringify() Method

Last Updated : 08 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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 module
const querystring = require("querystring");
  
// Specify the URL object
// to be serialized
let urlObject = {
    user: "sam",
    access: true,
    role: ["admin", "editor", "manager"],
};
  
// Use the stringify() method on the object
let 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 module
const querystring = require("querystring");
  
// Specify the URL object
// to be serialized
let 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



Previous Article
Next Article

Similar Reads

Node.js querystring.parse() Method
The querystring.parse() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding format is assumed unless there is an alternative charac
3 min read
Node.js querystring.escape() Method
The querystring.escape( ) function is used to produce a percent-encoded query string from a normal string. This method is very similar to the browser's encodeURIComponent functions. This method performs percent-encoding on the given string it means it encodes any string into a URL query string by using the % symbol. This method iterates through the
2 min read
Node.js querystring.unescape() Method
On the specified str, the querystring.unescape() method decodes URL percent-encoded characters. This method converts a percent-encoding string into a normal string. It means it decodes any percent-encoding string into a normal string by removing the % symbol. This method iterates through the string and removes the % encoding where needed on the spe
2 min read
Node.js querystring.encode() Function
The querystring.encode() method is used to produce a 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 boolean. Any other types of values are coerced to empty strings. During se
2 min read
Node.js querystring.decode() Function
The querystring.decode() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding format is assumed unless there is an alternative chara
2 min read
JavaScript JSON stringify() Method
The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object. Syntax: JSON.stringify(value, replacer, space);Parameters: value: It is the value that is to be converted into a JSON string.replacer: It is
3 min read
ASP Request.QueryString Collection
The ASP Request.QueryString Collection is used to get the value of the variable that will be stored in the HTTP query string from the URL. The Query string is a part of the URL that begins with a question mark (?). Generally, it contains the information of the user. <a href= "geek.asp?txt=this is a query string test">Link with a query string
1 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
How to JSON Stringify an Array of Objects in JavaScript ?
In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects. Table of Content Using JSON.stringify with a Replacer Function Using a Custom Function for JSON StringifyUsing a Custom
3 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
5 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
Encountering the error message ‘node’ is not recognized as an internal or external command after installing Node.js can be frustrating, especially when you’re eager to start working on your project. This error usually means that your system cannot find the Node.js executable because the path to it is not set correctly in your system's environment v
3 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
Node.js URLSearchParams.has() Method
In URLSearchParams interface, the has() method returns a Boolean which tells us that if the parameter with input name exists it will return true, else false. Syntax: var Bool = URLSearchParams.has(name) Returns: True - if name present, else it will return False. Parameters: name - Input the name of the parameter. Example1: let url = new URL('https:
1 min read
Node.js hmac.update() Method
The hmac.update() method is an inbuilt method of class HMAC within the crypto module which is used to update the data of hmac object. Syntax: hmac.update(data[, inputEncoding])Parameters: This method takes the following two parameters: data: It can be of string, Buffer, TypedArray, or DataView type. It is the data that is passed to this function.in
2 min read
Node.js process.send() Method
The process.send() method is an inbuilt application programming interface of the process module which is used by the child process to communicate with the parent process. This method does not work for the root process because it does not have any parent process. Syntax: process.send(message, [sendHandle])Parameters: This method accepts the followin
2 min read
Node.js writeStream.clearLine() Method
The writeStream.clearLine() is an inbuilt application programming interface of class WriteStream within the module which is used to clear the current line of this write stream object. Syntax: writeStream.clearLine(dir[, callback]) Parameters: This method takes the following argument as a parameter: dir: Integer value defining the selection of a lin
2 min read
Node.js net.SocketAddress() Method
Node.js Net module allows you to create TCP or IPC servers and clients. A TCP client initiates a connection request to a TCP server to establish a connection with the server. Syntax: Below is the syntax for importing the Net module into your node js project: const <Variable_Name> = require('node:net'); const Net_Module = require('node:net');
3 min read
Node.js fs.link() Method
The fs.link() method is used to create a hard link to the given path. The hard link created would still point to the same file even if the file is renamed. The hard links also contain the actual contents of the linked file. Syntax: fs.link( existingPath, newPath, callback ) Parameters: This method accepts three parameters as mentioned above and des
2 min read
Run Python script from Node.js using child process spawn() method
Node.js is one of the most adopted web development technologies but it lacks support for machine learning, deep learning and artificial intelligence libraries. Luckily, Python supports all these and many more other features. Django Framework for Python can utilize this functionality of Python and can provide support for building new age web applica
3 min read
Node.js date-and-time Date.isLeapYear() Method
The date-and-time.Date.isLeapYear() is a minimalist collection of functions for manipulating JS date and time module which is used to check if the given year is a leap year or not. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.m
2 min read
Node.js Http2ServerRequest.httpVersion Method
The Http2ServerRequest.httpVersion is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the HTTP version either associated with server or client. Syntax: const request.httpVersion Parameters: This method does not accept any argument as a parameter. Return Value: This method returns
3 min read
Node.js hmac.digest() Method
The hmac.digest() method is an inbuilt application programming interface of class hmac within crypto module which is used to return the hmac hash value of inputted data. Syntax: hmac.digest([encoding])Parameter: This method takes encoding as a parameter which is an optional parameter. Return Value: This method calculates the hmac digest of all data
2 min read
Node.js v8.Serializer.writeDouble() Method
The v8.Serializer.writeDouble() method is an inbuilt application programming interface of the v8.Serializer module which is used to write a JS number value to the internal buffer. For use inside of custom serializer._writeHostObject(). Syntax: v8.Serializer.writeDouble( Value ); Parameters: This method accepts single parameter as mentioned above an
1 min read
Node.js v8.Serializer.writeRawBytes() Method
The v8.Serializer.writeRawBytes() method is an inbuilt application programming interface of the v8.Serializer module which is used to write a raw buffer data to the internal buffer. For use inside of custom serializer._writeHostObject(). Syntax: v8.Serializer.writeRawBytes( Buffer ); Parameters: This method accepts single parameter as mentioned abo
1 min read
three90RightbarBannerImg