
- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js - Global Objects
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Built-in Modules
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - querystring.stringify() Method
The Node.js querystring.stringify() method will generate a URL query string from the provided object by iterating through the object’s own properties. This method can serialize the values passed in the object like string, number, bigint, Boolean, string[], number[], Boolean[], and bigint[].
While serializing, the query string's percent-encoded characters will by default be assumed to use UTF-8 encoding. The decodeURIComponent option has to be specified to decode alternative character encoding.
Syntax
Following is the syntax of the Node.js querystring.stringify() method −
querystring.stringify(obj[, sep[, eq[, options]]])
Parameters
This method accepts four parameters. Those are described below.
str − This parameter specifies an object to serialize into a URL query string.
sep − This parameter describes a substring which is used to define the keys and values pairs in the query string. The default value is ‘&’.
eq − This parameter describes a substring which is used to define the keys and values in the query string. The default value is ‘=’.
options − This is an object which will allow to modifying the behavior of the method. The following properties of this object are described below −
decodeURIComponent − This function is used to convert URL-unsafe characters to percent-encoding in the query string. By default, it is querystring.esacpe().
Return Value
This method returns a URL query string from the provided obj by iterating through the object’s “own properties”.
Example
If we specify an URL object to the Node.js querystring.stringify() method, it will return a serialized URL query string from the given object.
In the following example, we are passing an object with properties to Node.js querystring.stringify() method.
const { url } = require('node:inspector'); const querystring = require('node:querystring'); var QueryString = { User: 'john', role: 'softwareeng', tech: 'HTML', permission: 'false' } var result = querystring.stringify(QueryString); console.log(result);
Error
Following is the output of the above program −
internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'node:inspector' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a03fcfc3513/main.js:1:79) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
Note − To see actual result, execute the above code in local.
After executing the above program, the querystring.stringify() returns a URL string from the given object's own properties.
User=john&role=softwareeng&tech=HTML&permission=false
Example
If we pass any value to the sep and eq parameters of the Node.js querystring.stringify() method, it will return the URL query string from the object with the same delimitation.
In the example below, we are trying to provide && to sep and − to the eq parameters of the Node.js querystring.stringify() method.
const { url } = require('node:inspector'); const querystring = require('node:querystring'); var QueryString = { User: 'john', role: 'softwareeng', tech: 'HTML', permission: 'false' } var result = querystring.stringify(QueryString, "&&", "−"); console.log(result);
Error
Following is the output of the above program −
internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'node:inspector' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a03fcfc3513/main.js:1:79) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
Note − To see actual result, execute the above code in local.
After executing the above program, the querystring.stringify() returns a URL string with the same delimitation in it.
User−john&&role−softwareeng&&tech−HTML&&permission−false
Example
The Node.js querystring.stringify() method will serialize the returned URL query string even if we pass a finite integer value to the obj property.
In the program below, we are trying to pass an integer value in the obj.
const { url } = require('node:inspector'); const querystring = require('node:querystring'); var QueryString = { 4355: 4565, role: 'softwareeng', tech: 'HTML', permission: 'false' } var result = querystring.stringify(QueryString, "&&&", "==="); console.log(result);
Error
Following is the output of the above program −
internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'node:inspector' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a03fcfc3513/main.js:1:79) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
Note − To see actual result, execute the above code in local.
If you execute the above program, the output is displayed as follows.
4355===4565&&&role===softwareeng&&&tech===HTML&&&permission===false