VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-querystring-stringify-method/

⇱ Node.js querystring.stringify() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js querystring.stringify() Method

Last Updated : 8 Oct, 2021
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. During serializing, the UTF-8 encoding format is used to encode any character that requires percent-encoding. To encode using an alternative character encoding, the encodeURIComponent option has to be specified. Syntax:
querystring.stringify( obj[, sep[, eq[, options]]] )
Parameters: This function accepts four parameters as mentioned above and described below:
  • obj: It is an Object that has to be serialized into the URL query string.
  • sep: It is a String that specifies the substring used to delimit the key and value pairs in the query string. The default value is "&".
  • eq: It is a String that specifies the substring used to delimit keys and values in the query string. The default value is "=".
  • options: It is an Object which can be used to modify the behaviour of the method. It has the following parameters:
    • encodeURIComponent: It is a function that would be used to convert URL-unsafe characters to percent-encoding in the query string. The default value is querystring.escape().
Return Value: It returns a String that contains the URL query produced from the given object. Below programs illustrate the querystring.stringify() method in Node.js: Example 1: Output:
Parsed Query: user=sam&access=true&role=admin&role=editor&role=manager
Example 2: Output:
Parsed Query 1: user:max, access:false, role:editor, role:manager

Parsed Query 2: user==max&&&access==false&&&role==editor&&&role==manager
Reference: https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options
Comment

Explore