Open In App

Node.js querystring.parse() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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
 



Last Updated : 08 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads