Node.js querystring.parse() Method

08 Oct, 2021
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”.
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 moduleconst querystring = require("querystring");  // Specify the URL query string// to be parsedlet urlQuery =   "username=user1&units=kgs&units=pounds&permission=false";  // Use the parse() method on the stringlet 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 moduleconst querystring = require("querystring");  // Specify the URL query string// to be parsedlet urlQuery =   "user=admin&articles=1&articles=2&articles=3&access=true";  // Use the parse() method on the string// with default valueslet parsedObject = querystring.parse(urlQuery, "&", "=");  console.log("Parsed Query:", parsedObject);  // Use the parse() method on the string// with maxKeys set to 1parsedObject =   querystring.parse(urlQuery, "&", "=", { maxKeys: 1 });  console.log("\nParsed Query:", parsedObject);  // Use the parse() method on the string// with maxKeys set to 2parsedObject =   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 
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