Open In App

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 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: 

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:




// 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:




// 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
 


Article Tags :