Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
Gathering detailed insights and metrics for query-string
npm install query-string
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.4
Supply Chain
99.1
Quality
85.3
Maintenance
100
Vulnerability
100
License
JavaScript (93.91%)
TypeScript (6.09%)
Total Downloads
4,380,359,295
Last Day
685,383
Last Week
12,972,141
Last Month
56,506,192
Last Year
632,555,230
MIT License
6,849 Stars
268 Commits
453 Forks
30 Watchers
1 Branches
72 Contributors
Updated on Jun 06, 2025
Minified
Minified + Gzipped
Latest Version
9.2.0
Package Id
query-string@9.2.0
Unpacked Size
51.59 kB
Size
10.31 kB
File Count
7
NPM Version
10.9.2
Node Version
20.19.1
Published on
May 20, 2025
Cumulative downloads
Total Downloads
Last Day
-12.4%
685,383
Compared to previous day
Last Week
-8%
12,972,141
Compared to previous week
Last Month
2.9%
56,506,192
Compared to previous month
Last Year
-2.6%
632,555,230
Compared to previous year
3
6
Parse and stringify URL query strings
1npm 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.
1import queryString from 'query-string'; 2 3console.log(location.search); 4//=> '?foo=bar' 5 6const parsed = queryString.parse(location.search); 7console.log(parsed); 8//=> {foo: 'bar'} 9 10console.log(location.hash); 11//=> '#token=bada55cafe' 12 13const parsedHash = queryString.parse(location.hash); 14console.log(parsedHash); 15//=> {token: 'bada55cafe'} 16 17parsed.foo = 'unicorn'; 18parsed.ilike = 'pizza'; 19 20const stringified = queryString.stringify(parsed); 21//=> 'foo=unicorn&ilike=pizza' 22 23location.search = stringified; 24// note that `location.search` automatically prepends a question mark 25console.log(location.search); 26//=> '?foo=unicorn&ilike=pizza'
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
.
Type: object
Type: boolean
Default: true
Decode the keys and values. URL components are decoded with decode-uri-component
.
Type: string
Default: 'none'
'bracket'
: Parse arrays with bracket representation:1import queryString from 'query-string'; 2 3queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); 4//=> {foo: ['1', '2', '3']}
'index'
: Parse arrays with index representation:1import queryString from 'query-string'; 2 3queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); 4//=> {foo: ['1', '2', '3']}
'comma'
: Parse arrays with elements separated by comma:1import queryString from 'query-string'; 2 3queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); 4//=> {foo: ['1', '2', '3']}
'separator'
: Parse arrays with elements separated by a custom character:1import queryString from 'query-string'; 2 3queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); 4//=> {foo: ['1', '2', '3']}
'bracket-separator'
: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:1import queryString from 'query-string'; 2 3queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 4//=> {foo: []} 5 6queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 7//=> {foo: ['']} 8 9queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 10//=> {foo: ['1']} 11 12queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 13//=> {foo: ['1', '2', '3']} 14 15queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 16//=> {foo: ['1', '', 3, '', '', '6']} 17 18queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 19//=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
'colon-list-separator'
: Parse arrays with parameter names that are explicitly marked with :list
:1import queryString from 'query-string'; 2 3queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'}); 4//=> {foo: ['one', 'two']}
'none'
: Parse arrays with elements using duplicate keys:1import queryString from 'query-string'; 2 3queryString.parse('foo=1&foo=2&foo=3'); 4//=> {foo: ['1', '2', '3']}
Type: string
Default: ','
The character used to separate array elements when using {arrayFormat: 'separator'}
.
Type: Function | boolean
Default: true
Supports both Function
as a custom sorting function or false
to disable sorting.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.parse('foo=1', {parseNumbers: true}); 4//=> {foo: 1}
Parse the value as a number type instead of string type if it's a number.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.parse('foo=true', {parseBooleans: true}); 4//=> {foo: true}
Parse the value as a boolean type instead of string type if it's a boolean.
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.
Supported Types:
'boolean'
: Parse flagged
as a boolean (overriding the parseBooleans
option):1queryString.parse('?isAdmin=true&flagged=true&isOkay=0', { 2 parseBooleans: false, 3 types: { 4 flagged: 'boolean', 5 isOkay: 'boolean', 6 }, 7}); 8//=> {isAdmin: 'true', flagged: true, isOkay: false}
Note: The 'boolean'
type will also convert '0'
and '1'
string values to booleans.
'string'
: Parse phoneNumber
as a string (overriding the parseNumbers
option):1import queryString from 'query-string'; 2 3queryString.parse('?phoneNumber=%2B380951234567&id=1', { 4 parseNumbers: true, 5 types: { 6 phoneNumber: 'string', 7 } 8}); 9//=> {phoneNumber: '+380951234567', id: 1}
'number'
: Parse age
as a number (even when parseNumbers
is false):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: 'number', 6 } 7}); 8//=> {age: 20, id: '01234', zipcode: '90210'}
'string[]'
: Parse items
as an array of strings (overriding the parseNumbers
option):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&items=1%2C2%2C3', { 4 parseNumbers: true, 5 types: { 6 items: 'string[]', 7 } 8}); 9//=> {age: 20, items: ['1', '2', '3']}
'number[]'
: Parse items
as an array of numbers (even when parseNumbers
is false):1import queryString from 'query-string'; 2 3queryString.parse('?age=20&items=1%2C2%2C3', { 4 types: { 5 items: 'number[]', 6 } 7}); 8//=> {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.1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: (value) => value * 2, 6 } 7}); 8//=> {age: 40, id: '01234', zipcode: '90210'}
NOTE: Array types (string[]
, number[]
) are ignored if arrayFormat
is set to 'none'
.
1queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', { 2 arrayFormat: 'none', 3 types: { 4 ids: 'number[]', 5 foods: 'string[]', 6 }, 7} 8//=> {ids:'001,002,003', foods:'apple,orange,mango'}
1import queryString from 'query-string'; 2 3queryString.parse('?age=20&id=01234&zipcode=90210', { 4 types: { 5 age: (value) => value * 2, 6 } 7}); 8//=> {age: 40, id: '01234', zipcode: '90210'}
Parse the value as a boolean type instead of string type if it's a boolean.
Stringify an object into a query string and sorting the keys.
Type: object
Type: boolean
Default: true
Strictly encode URI components. It uses encodeURIComponent if set to false. You probably don't care about this option.
Type: boolean
Default: true
URL encode the keys and values.
Type: string
Default: 'none'
'bracket'
: Serialize arrays using bracket representation:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); 4//=> 'foo[]=1&foo[]=2&foo[]=3'
'index'
: Serialize arrays using index representation:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); 4//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
'comma'
: Serialize arrays by separating elements with comma:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); 4//=> 'foo=1,2,3' 5 6queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'}); 7//=> 'foo=1,,' 8// Note that typing information for null values is lost 9// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
'separator'
: Serialize arrays by separating elements with a custom character:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'}); 4//=> 'foo=1|2|3'
'bracket-separator'
: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:1import queryString from 'query-string'; 2 3queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 4//=> 'foo[]' 5 6queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 7//=> 'foo[]=' 8 9queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 10//=> 'foo[]=1' 11 12queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 13//=> 'foo[]=1|2|3' 14 15queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 16//=> 'foo[]=1||3|||6' 17 18queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true}); 19//=> 'foo[]=1||3|6' 20 21queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); 22//=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
'colon-list-separator'
: Serialize arrays with parameter names that are explicitly marked with :list
:1import queryString from 'query-string'; 2 3queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'}); 4//=> 'foo:list=one&foo:list=two'
'none'
: Serialize arrays by using duplicate keys:1import queryString from 'query-string'; 2 3queryString.stringify({foo: [1, 2, 3]}); 4//=> 'foo=1&foo=2&foo=3'
Type: string
Default: ','
The character used to separate array elements when using {arrayFormat: 'separator'}
.
Type: Function | boolean
Supports both Function
as a custom sorting function or false
to disable sorting.
1import queryString from 'query-string'; 2 3const order = ['c', 'a', 'b']; 4 5queryString.stringify({a: 1, b: 2, c: 3}, { 6 sort: (a, b) => order.indexOf(a) - order.indexOf(b) 7}); 8//=> 'c=3&a=1&b=2'
1import queryString from 'query-string'; 2 3queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); 4//=> 'b=1&c=2&a=3'
If omitted, keys are sorted using Array#sort()
, which means, converting them to strings and comparing strings in Unicode code point order.
Skip keys with null
as the value.
Note that keys with undefined
as the value are always skipped.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.stringify({a: 1, b: undefined, c: null, d: 4}, { 4 skipNull: true 5}); 6//=> 'a=1&d=4'
1import queryString from 'query-string'; 2 3queryString.stringify({a: undefined, b: null}, { 4 skipNull: true 5}); 6//=> ''
Skip keys with an empty string as the value.
Type: boolean
Default: false
1import queryString from 'query-string'; 2 3queryString.stringify({a: 1, b: '', c: '', d: 4}, { 4 skipEmptyString: true 5}); 6//=> 'a=1&d=4'
1import queryString from 'query-string'; 2 3queryString.stringify({a: '', b: ''}, { 4 skipEmptyString: true 5}); 6//=> ''
Extract a query string from a URL that can be passed into .parse()
.
Note: This behaviour can be changed with the skipNull
option.
Extract the URL and the query string as an object.
Returns an object with a url
and query
property.
If the parseFragmentIdentifier
option is true
, the object will also contain a fragmentIdentifier
property.
1import queryString from 'query-string';
2
3queryString.parseUrl('https://foo.bar?foo=bar');
4//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
5
6queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
7//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
Type: object
The options are the same as for .parse()
.
Extra options are as below.
Parse the fragment identifier from the URL.
Type: boolean
Default: false
1import queryString from 'query-string';
2
3queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
4//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
Stringify an object into a URL with a query string and sorting the keys. The inverse of .parseUrl()
The options
are the same as for .stringify()
.
Returns a string with the URL and a query string.
Query items in the query
property overrides queries in the url
property.
The fragmentIdentifier
property overrides the fragment identifier in the url
property.
1queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
2//=> 'https://foo.bar?foo=bar'
3
4queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
5//=> 'https://foo.bar?foo=bar'
6
7queryString.stringifyUrl({
8 url: 'https://foo.bar',
9 query: {
10 top: 'foo'
11 },
12 fragmentIdentifier: 'bar'
13});
14//=> 'https://foo.bar?top=foo#bar'
Type: object
Type: string
The URL to stringify.
Type: object
Query items to add to the URL.
Pick query parameters from a URL.
Returns a string with the new URL.
1import queryString from 'query-string'; 2 3queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']); 4//=> 'https://foo.bar?foo=1#hello' 5 6queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true}); 7//=> 'https://foo.bar?bar=2#hello'
Exclude query parameters from a URL.
Returns a string with the new URL.
1import queryString from 'query-string'; 2 3queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']); 4//=> 'https://foo.bar?bar=2#hello' 5 6queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true}); 7//=> 'https://foo.bar?foo=1#hello'
Type: string
The URL containing the query parameters to filter.
Type: string[]
The names of the query parameters to filter based on the function used.
Type: (key, value) => boolean
A filter predicate that will be provided the name of each query parameter and its value. The parseNumbers
and parseBooleans
options also affect value
.
Type: object
Parse options and stringify options.
This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of edge cases.
You're much better off just converting the object to a JSON string:
1import queryString from 'query-string'; 2 3queryString.stringify({ 4 foo: 'bar', 5 nested: JSON.stringify({ 6 unicorn: 'cake' 7 }) 8}); 9//=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
However, there is support for multiple instances of the same key:
1import queryString from 'query-string'; 2 3queryString.parse('likes=cake&name=bob&likes=icecream'); 4//=> {likes: ['cake', 'icecream'], name: 'bob'} 5 6queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'}); 7//=> 'color=taupe&color=chartreuse&id=515'
Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
1import queryString from 'query-string'; 2 3queryString.stringify({foo: false}); 4//=> 'foo=false' 5 6queryString.stringify({foo: null}); 7//=> 'foo' 8 9queryString.stringify({foo: undefined}); 10//=> ''
+
as a space?See this answer.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
project is fuzzed
Details
Reason
6 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-26
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More