MCPcopy Create free account
hub / github.com/sindresorhus/query-string

github.com/sindresorhus/query-string

Chat with this repo
repository ↗ · DeepWiki ↗ · release v10.0.0 ↗ · + Follow · compare 4 versions
48 symbols 127 edges 14 files ⚖ MIT 0 documented · 0% ● updated 34d agov10.0.0 · 2026-10-21★ 6,9191 open issues

Browse by type

⨍ Functions 46◇ Types & classes 2
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

query-string

Parse and stringify URL query strings

Install

npm install query-string

[!WARNING] Remember the hyphen! Do not install the deprecated querystring package!

For browser usage, this package targets the latest version of Chrome, Firefox, and Safari.

[!TIP] Consider using URLSearchParams for simple use cases. It's a native browser API that handles basic query string operations.

Usage

import queryString from 'query-string';

console.log(location.search);
//=> '?foo=bar'

const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}

console.log(location.hash);
//=> '#token=bada55cafe'

const parsedHash = queryString.parse(location.hash);
console.log(parsedHash);
//=> {token: 'bada55cafe'}

parsed.foo = 'unicorn';
parsed.ilike = 'pizza';

const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'

location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'

API

.parse(string, options?)

Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.

The returned object is created with Object.create(null) and thus does not have a prototype.

queryString.parse('?foo=bar');
//=> {foo: 'bar'}

queryString.parse('#token=secret&name=john');
//=> {name: 'john', token: 'secret'}

options

Type: object

decode

Type: boolean\ Default: true

Decode the keys and values. A byte that is not valid UTF-8 becomes the replacement character U+FFFD, and the bytes around it are still decoded, like the URL parser and URLSearchParams do. A % that is not followed by two hexadecimal digits is not an escape and is kept as it is.

arrayFormat

Type: string\ Default: 'none'

  • 'bracket': Parse arrays with bracket representation:
import queryString from 'query-string';

queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
//=> {foo: ['1', '2', '3']}
  • 'index': Parse arrays with index representation:
import queryString from 'query-string';

queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
//=> {foo: ['1', '2', '3']}

Entries without an index, such as foo[], are appended after the highest index seen so far. Appending after an index greater than or equal to Number.MAX_SAFE_INTEGER is unsupported.

  • 'comma': Parse arrays with elements separated by comma:
import queryString from 'query-string';

queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
//=> {foo: ['1', '2', '3']}

queryString.parse('foo=1,2&foo=3', {arrayFormat: 'comma'});
//=> {foo: ['1', '2', '3']}

A single value has no separator, so it is a plain value, not an array with one element. Use the types option to always get an array, or use 'bracket-separator', which marks arrays explicitly.

import queryString from 'query-string';

queryString.parse('foo=1', {arrayFormat: 'comma'});
//=> {foo: '1'}

queryString.parse('foo=1', {arrayFormat: 'comma', types: {foo: 'string[]'}});
//=> {foo: ['1']}
  • 'separator': Parse arrays with elements separated by a custom character:
import queryString from 'query-string';

queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
//=> {foo: ['1', '2', '3']}
  • 'bracket-separator': Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
import queryString from 'query-string';

queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {foo: []}

queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {foo: ['']}

queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {foo: ['1']}

queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {foo: ['1', '2', '3']}

queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {foo: ['1', '', '3', '', '', '6']}

queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> {bar: 'fluffy', baz: ['4'], foo: ['1', '2', '3']}
  • 'colon-list-separator': Parse arrays with parameter names that are explicitly marked with :list:
import queryString from 'query-string';

queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'});
//=> {foo: ['one', 'two']}
  • 'none': Parse arrays with elements using duplicate keys:
import queryString from 'query-string';

queryString.parse('foo=1&foo=2&foo=3');
//=> {foo: ['1', '2', '3']}
arrayFormatSeparator

Type: string\ Default: ','

The character used to separate array elements when using {arrayFormat: 'separator'}.

Only a literal separator delimits array elements. A percent-encoded separator (for example, %2C) is part of the element, which is how .stringify() encodes elements that contain the separator. Because of this, % and a hexadecimal digit are unsupported, since those also appear inside percent-encoded elements, and so are &, +, and #, since those already have a meaning in a URL: & separates parameters, + is decoded as a space, and # starts the fragment identifier. Those are handled on a best-effort basis, so the result is undefined. Whitespace cannot work at all, since leading and trailing whitespace is ignored when parsing, and a lone surrogate cannot be percent-encoded, so both throw a TypeError.

import queryString from 'query-string';

queryString.parse('foo=a%2Cb,c', {arrayFormat: 'comma'});
//=> {foo: ['a,b', 'c']}

queryString.parse('foo[]=a%2Cb,c', {arrayFormat: 'bracket-separator'});
//=> {foo: ['a,b', 'c']}
sort

Type: Function | boolean\ Default: true

Supports both Function as a custom sorting function or false to disable sorting.

If omitted, keys are sorted using Array#sort().

parseNumbers

Type: boolean\ Default: false

import queryString from 'query-string';

queryString.parse('foo=1', {parseNumbers: true});
//=> {foo: 1}

Parse the value as a number type instead of string type if it's a number.

parseBooleans

Type: boolean\ Default: false

import queryString from 'query-string';

queryString.parse('foo=true', {parseBooleans: true});
//=> {foo: true}

Parse the value as a boolean type instead of string type if it's a boolean.

types

Type: object\ Default: {}

Specifies a schema for parsing query values with explicit type declarations. When defined, the types provided here take precedence over general parsing options such as parseNumbers, parseBooleans, and arrayFormat.

Use this option to explicitly define the type of a specific parameter—particularly useful in cases where the type might otherwise be ambiguous (e.g., phone numbers or IDs).

You can also provide a custom function to transform the value. The function will receive the raw string and should return the desired parsed result. When used with array formats (like comma, separator, bracket, etc.), the function is applied to each array element individually.

Supported Types:

  • 'boolean': Parse flagged as a boolean (overriding the parseBooleans option):
import queryString from 'query-string';

queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
    parseBooleans: false,
    types: {
        flagged: 'boolean',
        isOkay: 'boolean',
    },
});
//=> {flagged: true, isAdmin: 'true', isOkay: false}

Note: The 'boolean' type also converts '0' and '1' to booleans, and treats valueless keys (e.g. ?flag) as true.

  • 'string': Parse phoneNumber as a string (overriding the parseNumbers option):
import queryString from 'query-string';

queryString.parse('?phoneNumber=%2B380951234567&id=1', {
    parseNumbers: true,
    types: {
        phoneNumber: 'string',
    }
});
//=> {id: 1, phoneNumber: '+380951234567'}
  • 'number': Parse age as a number (even when parseNumbers is false):
import queryString from 'query-string';

queryString.parse('?age=20&id=01234&zipcode=90210', {
    types: {
        age: 'number',
    }
});
//=> {age: 20, id: '01234', zipcode: '90210'}
  • 'string[]': Parse items as an array of strings (overriding the parseNumbers option):
import queryString from 'query-string';

queryString.parse('?age=20&items=1,2,3', {
    arrayFormat: 'comma',
    parseNumbers: true,
    types: {
        items: 'string[]',
    }
});
//=> {age: 20, items: ['1', '2', '3']}
  • 'number[]': Parse items as an array of numbers (even when parseNumbers is false):
import queryString from 'query-string';

queryString.parse('?age=20&items=1,2,3', {
    arrayFormat: 'comma',
    types: {
        items: 'number[]',
    }
});
//=> {age: '20', items: [1, 2, 3]}
  • 'Function': Provide a custom function as the parameter type. The parameter's value will equal the function's return value. When used with array formats (like comma, separator, bracket, etc.), the function is applied to each array element individually.
import queryString from 'query-string';

queryString.parse('?age=20&id=01234&zipcode=90210', {
    types: {
        age: value => value * 2,
    }
});
//=> {age: 40, id: '01234', zipcode: '90210'}

// With arrays, the function is applied to each element
queryString.parse('?scores=10,20,30', {
    arrayFormat: 'comma',
    types: {
        scores: value => Number(value) * 2,
    }
});
//=> {scores: [20, 40, 60]}

NOTE: With arrayFormat: 'none', array types (string[], number[]) do not turn a single value into an array. The value stays a string, and parseNumbers and parseBooleans do not apply to it. A repeated key is still an array, and its items are parsed with the element type.

queryString.parse('ids=001,002,003&foods=apple,orange,mango', {
    arrayFormat: 'none',
    types: {
        ids: 'number[]',
        foods: 'string[]',
    },
});
//=> {foods: 'apple,orange,mango', ids: '001,002,003'}

.stringify(object, options?)

Stringify an object into a query string and sorting the keys.

Supported value types: string, number, bigint, boolean, null, undefined, and arrays of these types. Other types like Symbol, functions, or objects (except arrays) will throw an error.

options

Type: object

strict

Type: boolean\ Default: true

Strictly encode URI components. It uses encodeURIComponent if set to false. You probably don't care about this option.

encode

Type: boolean\ Default: true

URL encode the keys and values.

A lone surrogate, which cannot be percent-encoded, is replaced with the replacement character U+FFFD, like URLSearchParams does.

arrayFormat

Type: string\ Default: 'none'

  • 'bracket': Serialize arrays using bracket representation:
import queryString from 'query-string';

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
//=> 'foo[]=1&foo[]=2&foo[]=3'
  • 'index': Serialize arrays using index representation:
import queryString from 'query-string';

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
  • 'comma': Serialize arrays by separating elements with comma:
import queryString from 'query-string';

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
//=> 'foo=1,2,3'

queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
//=> 'foo=1,,'
// Note that typing information for null values is lost
// and `.parse('foo=1,,', {arrayFormat: 'comma'})` would return `{foo: ['1', '', '']}`.
  • 'separator': Serialize arrays by separating elements with a custom character:
import queryString from 'query-string';

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
//=> 'foo=1|2|3'
  • 'bracket-separator': Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
import queryString from 'query-string';

queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'foo[]'

queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'foo[]='

queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'foo[]=1'

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'foo[]=1|2|3'

queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'foo[]=1||3|||6'

queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
//=> 'foo[]=1||3|6'

queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
//=> 'bar=fluffy&baz[]=4&foo[]=1|2|3'
  • 'colon-list-separator': Serialize arrays with parameter names that are explicitly marked with

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Function 46
Interface 2

Languages

TypeScript100%

Modules by API surface

base.js28 symbols
test/stringify.js6 symbols
index.test-d.ts6 symbols
test/parse.js5 symbols
test/stringify-url.js1 symbols
test/properties.js1 symbols
benchmark.js1 symbols

Dependencies from manifests, versioned

deep-equal2.2.3 · 1×
fast-check4.10.2 · 1×
filter-obj6.1.0 · 1×
nz0.1.0 · 1×
split-on-first4.0.0 · 1×
xo5.0.1 · 1×

For agents

$ claude mcp add query-string \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page