explaingit

ljharb/qs

8,930JavaScriptAudience · developerComplexity · 1/5LicenseSetup · easy

TLDR

qs parses and serializes URL query strings in JavaScript, with support for nested objects, arrays, and built-in safety limits that prevent abuse from oversized or deeply-nested inputs.

Mindmap

mindmap
  root((repo))
    What It Does
      Parse query strings
      Serialize to URL
      Nested object support
      Safety limits built in
    Tech Stack
      JavaScript
      Node.js
      Browser compatible
    Use Cases
      API request building
      Form data handling
      Legacy encoding support
    Features
      Bracket notation
      Array formatting
      Custom delimiters
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Parse a URL query string from user input into a nested JavaScript object, safely handling unexpected or malformed values.

USE CASE 2

Serialize a nested JavaScript object into a URL query string with bracket notation for use in an API request.

USE CASE 3

Handle arrays and nested structures in query strings that the built-in browser URLSearchParams cannot represent.

USE CASE 4

Enforce parsing limits in a server-side app to prevent abuse from oversized or deeply-nested query string inputs.

Tech stack

JavaScriptNode.js

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose including commercial, keep the copyright notice and do not use the author's name to endorse your product.

In plain English

qs is a small JavaScript library for reading and writing query strings, which are the key-value pairs that appear after the question mark in a URL. For example, in the address example.com/search?name=alice&age=30, the part after the question mark is a query string. qs gives you a clean way to convert that text into a JavaScript object and, in the other direction, to turn an object back into a query string. What sets qs apart from the basic query string tools built into browsers and Node.js is support for nesting. You can represent structured data like { user: { name: 'alice' } } in a URL using bracket notation (user[name]=alice), and qs will correctly parse it back into that nested shape. It also handles arrays, optional dot notation, and several edge cases around duplicate keys and special characters. The library includes a number of safety features designed for situations where the query string comes from untrusted input. By default it limits parsing to 1,000 parameters and five levels of nesting, which prevents certain kinds of abuse that can occur when an attacker sends an abnormally large or deeply nested input. These limits are configurable, and you can also set the library to throw an error rather than silently truncate when a limit is exceeded. The README is detailed and covers many options: ignoring the leading question mark, using custom delimiters, handling different character encodings (including older ISO-8859-1 encoding used by some legacy systems), controlling how arrays are formatted in the output, and more. Both parsing and stringifying have their own set of options. qs works in both Node.js and browsers, and is widely used as a dependency inside larger frameworks and tools. It is maintained under the BSD-3-Clause license.

Copy-paste prompts

Prompt 1
Show me how to use qs to parse a query string with nested user data like user[name]=alice and user[age]=30 into a JavaScript object.
Prompt 2
Write a Node.js Express middleware that uses qs to parse request query strings with custom depth and parameter limits for security.
Prompt 3
How do I use qs.stringify to convert a JavaScript object with arrays into a URL-safe query string, controlling how arrays are formatted?
Prompt 4
Help me configure qs to use dot notation instead of bracket notation and handle ISO-8859-1 encoded legacy form data.
Open on GitHub → Explain another repo

← ljharb on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.