Open In App

Node.js querystring.parse() Method

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 character encoding format. To decode alternative character encoding, the decodeURIComponent option has to be specified.
 

Syntax: 

querystring.parse( str[, sep[, eq[, options]]]) )

Parameters: This function accepts four parameters as mentioned above and described below: 

  • str: It is a String that specifies the URL query that has to be parsed.
  • sep: It is a String that specifies the substring used to delimit the key and value pairs in the specified query string. The default value is “&”.
  • eq: It is a String that specifies the substring used to delimit keys and values in the specified query string. The default value is “=”.
  • options: It is an object which can be used to modify the behavior of the method. It has the following parameters: 
    • decodeURIComponent: It is a function that would be used to decode percent-encoded characters in the query string. The default value is querystring.unescape().
    • maxKeys: It is a number which specifies the maximum number of keys that should be parsed from the query string. A value of “0” would remove all the counting limits. The default value is “1000”.

Return Value: It returns an object that has the key and value pairs parsed from the query string.

Below examples illustrate the querystring.parse() method in Node.js:

Example 1:

Node.js




// Import the querystring module
const querystring = require("querystring");
  
// Specify the URL query string
// to be parsed
let urlQuery = 
  "username=user1&units=kgs&units=pounds&permission=false";
  
// Use the parse() method on the string
let parsedObject = querystring.parse(urlQuery);
  
console.log("Parsed Query:", parsedObject);
  
// Use the parse() method on the string
// with sep as `&&` and eq as `-`
urlQuery = 
  "username-user1&&units-kgs&&units-pounds&&permission-false";
parsedObject = querystring.parse(urlQuery, "&&", "-");
  
console.log("\nParsed Query:", parsedObject);


Output:

Parsed Query: [Object: null prototype] {
  username: 'user1',
  units: [ 'kgs', 'pounds' ],
  permission: 'false'
}

Parsed Query: [Object: null prototype] {
  username: 'user1',
  units: [ 'kgs', 'pounds' ],
  permission: 'false'
}

Example 2:

Node.js




// Import the querystring module
const querystring = require("querystring");
  
// Specify the URL query string
// to be parsed
let urlQuery = 
  "user=admin&articles=1&articles=2&articles=3&access=true";
  
// Use the parse() method on the string
// with default values
let parsedObject = querystring.parse(urlQuery, "&", "=");
  
console.log("Parsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 1
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 1 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 2
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 2 });
  
console.log("\nParsed Query:", parsedObject);
  
// Use the parse() method on the string
// with maxKeys set to 0 (no limits)
parsedObject = 
  querystring.parse(urlQuery, "&", "=", { maxKeys: 0 });
  
console.log("\nParsed Query:", parsedObject);


Output:

Parsed Query: [Object: null prototype] {
  user: 'admin',
  articles: [ '1', '2', '3' ],
  access: 'true'
}

Parsed Query: [Object: null prototype] { user: 'admin' }

Parsed Query: [Object: null prototype] 
              { user: 'admin', articles: '1' }

Parsed Query: [Object: null prototype] {
  user: 'admin',
  articles: [ '1', '2', '3' ],
  access: 'true'
}

Reference: https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options
 



Previous Article
Next Article

Similar Reads

Node.js querystring.stringify() Method
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. Duri
2 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
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
Node.js path.parse() Method
The path.parse() method is used to return an object whose properties represent the given path. This method returns the following properties: root (root name) dir (directory name) base (filename with extension) ext (only extension) name (only filename) The values of these properties may be different for every platform. It ignores the platform's trai
2 min read
Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API
The url.parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties. Syntax: url.parse( urlString, parseQueryString, slashesDenoteHost) Parameters: This method accepts three parameters as mentioned above and described below: urlString: It holds the URL string which needs to parse.parseQu
2 min read
Node.js Date.parse() API
The date-and-time.Date.parse() is a minimalist collection of functions for manipulating JS date and time module which is used to parse the date according to a certain pattern. Required Module: Install the module by npm or used it locally. By using npm.npm install date-and-time --saveBy using CDN link.<script src="/path/to/date-and-time.min.js"
2 min read
How to parse command line arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed. In Nodejs, these arguments are accessible through an array known as `argv` (arguments values), where the shell passes all received command-line arguments to the running process. We will use two
3 min read