Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js querystring.decode() Function

  • Last Updated : 08 Mar, 2021

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

Syntax:  

querystring.decode( str, sep, eq, options )

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

  • str: String that specifies the URL query that has to be parsed.
  • sep: String that specifies the substring used to delimit the key and value pairs in the specified query string. The default value is “&”.
  • eq: 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.

Example 1:

Javascript




const querystring = require('querystring');
  
let str = "user=pratik&isMale=true&role=Coder";
  
let output = querystring.decode(str);
  
console.log("Output: ", output);

Output:

Example 2:

Javascript




const querystring = require('querystring');
  
let str = "user->pratik/isMale->true/role->Coder/role->Engineer";
  
let output = querystring.decode(str, '/', '->');
  
console.log("Output 1 : ", output);
  
output = querystring.decode(str, '/', '->', {maxKeys: 2});
  
console.log("Output 2 : ", output);

Output:


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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!