Definition

Whenever the situation arises when we need to parse between query string and JSON object then we use ‘query string’ method which is available in ‘query string’ module. Querystring is the function which is only used to parse the object from JSON object to query string and vice versa.

Node.js Query String Methods

The Node.js Query String utility has four methods. The methods are given below  –

Name

Method

Description

 querystring.parse()querystring.parse(str_obj[,sep][, equation] [ options])converts query string into JSON object
 querystring.stringify() querystring.stringify(object[,sep] [,equation][,  options]) converts JSON object into  query string.
 querystring.escape() querystring.escape(str_obj)Returns an escaped query  string value.
 querystring.unescape()querystring.escape(str_obj)Returns an unescaped query string value.

queryString Parse

QueryString.parse() method converts query string into JSON object.

var querystring = require('querystring');
var q = querystring.parse('name=Kushal Thadani&company=Magnet Brain Software Technology PVT LTD');
// QueryString.Parse() method converts query string into JSOn Object
console.log(q);

queryString Stringify

QueryString.stringify() method converts JSON object into query string.

var querystring = require('querystring');
var obj = querystring.stringify({ Name: 'Kushal Thadani', company: 'Magnet Brain Software Technology PVT LTD' });
// QueryString.Stringify() Method Converts Object Into Query String
console.log(obj);

queryString Escape

The ‘queryString.escape()’ method performs URL percent – encoding on the given str in a manner that it improves the exact demand of URL query strings.
The ‘queryString.escape()’ method is used by ‘queryString.stringify()’ and is generally not familiar to be used directly. It is being transported primarily to allow application code to provide a changeable percent -encoding implementation, if required by assigning ‘queryString.escape’ to an alternative Function.

var querystring = require('querystring');
var real_value = 'http://example.com/product/demo.html';
var unescaped = queryString.unescape(escaped);
// http://example.com/product/demo.html
console.log(unescaped);

queryString Unescape

The queryString.unescape() method performs decoding of URL percent-encoded characters on the given str.

The queryString.unescape() method is used by querystring.parse() and is generally not familiar to be used directly. It is being imported primarily to allow application code to give a changeable decoding implementation if required by assigning queryString.unescape to an alternative function.

var querystring = require('querystring');
var real_value = 'http://example.com/product/demo.html';
var unescaped = queryString.unescape(escaped);
// http://example.com/product/demo.html
console.log(unescaped);

Learn More-