qs: Querystring Parser and Stringifier

6.15.1 · active · verified Tue Apr 21

qs is a robust JavaScript library for parsing and stringifying URL query strings, with comprehensive support for nesting objects and arrays. It is currently on version 6.15.1 and maintains a steady release cadence with a focus on stability and security patches. Key differentiators include its configurable depth limits for parsing, the ability to handle URI-encoded strings, and built-in protections against prototype pollution through options like `plainObjects` and `allowPrototypes` (which is dangerous if enabled). Unlike the native `querystring` module in Node.js, `qs` offers more advanced features like array indexing and custom parsing/stringifying logic, making it suitable for complex data structures often found in web applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic `qs.parse` and `qs.stringify` functionality, including nested objects, arrays, and the default depth limit behavior.

const qs = require('qs');
const assert = require('assert');

// Parsing a simple querystring
let obj = qs.parse('a=c&b=d');
assert.deepEqual(obj, { a: 'c', b: 'd' });

// Stringifying an object
let str = qs.stringify({ a: 'c', b: 'd' });
assert.equal(str, 'a=c&b=d');

// Parsing a nested object
obj = qs.parse('foo[bar]=baz');
assert.deepEqual(obj, { foo: { bar: 'baz' } });

// Parsing an array
obj = qs.parse('a=b&a=c');
assert.deepEqual(obj, { a: ['b', 'c'] });

// Demonstrating depth limit (default 5)
const deepString = 'a[b][c][d][e][f][g]=h';
obj = qs.parse(deepString);
assert.deepEqual(obj, {
    a: {
        b: { c: { d: { e: { f: { '[g]': 'h' } } } } }
    }
});

console.log('All assertions passed!');

view raw JSON →