Parse Query String into Object
Parses a query string (optionally starting with ?
) into a JavaScript object.
Supports arrays for duplicate keys, and can intelligently convert string values to primitives (numbers, booleans, null).
Importβ
import { parseQueryString } from 'nhb-toolbox';
import { getQueryStringAsObject } from 'nhb-toolbox';
import { queryStringToObject } from 'nhb-toolbox';
Function Signatureβ
function parseQueryString(
query: string,
parsePrimitives?: boolean
): StrictObject
Usage Examplesβ
- Basic
- With '?' Prefix
- Multiple Values
- No Primitive Parsing
- Null and Booleans
parseQueryString('a=1&b=hello');
// Returns: { a: 1, b: 'hello' }
parseQueryString('?token=abc&active=true');
// Returns: { token: 'abc', active: true }
parseQueryString('tag=js&tag=ts');
// Returns: { tag: ['js', 'ts'] }
parseQueryString('n=10&x=true', false);
// Returns: { n: '10', x: 'true' }
parseQueryString('opt=null&isTrue=false');
// Returns: { opt: null, isTrue: false }
API Referenceβ
Parametersβ
Name | Type | Description |
---|---|---|
query | string | The query string to parse (with or without starting ? ). |
parsePrimitives | boolean | (Optional) Whether to parse primitive-like strings ("false" , "1" , "null" , etc). Defaults to true . |
Returnsβ
A JavaScript object (StrictObject
) where each key is a string and each value may be:
string
number
boolean
null
string[]
,number[]
,boolean[]
, ornull[]
Typesβ
export type StrictObject = Record<string, unknown>;
Key Featuresβ
- Flexible Input: Accepts query strings with or without a leading
?
. - Arrays from Multiple Keys: If a key appears more than once, you get an array.
- Automatic Primitive Parsing: Converts
"1" β 1"
,"true" β true"
,"null" β null"
by default. - Safe: Does not touch or depend on the browserβs
window.location
.
Aliasesβ
This function is also exported as:
getQueryStringAsObject
queryStringToObject
Limitationsβ
- No Deep Objects: Does not generate nested objects for dotted or bracketed keys (e.g.,
a.b=1
stays flat). - Empty Key Handling: Keys without a value (e.g.,
foo&b=2
) will be parsed withfoo
as an empty string""
. - No Key Decoding Customization: Uses standard URL decoding.
Notesβ
- Type Guessing: Primitive parsing is based on string matching;
"012"
becomes number12
, not"012"
unlessparsePrimitives = false
. - Setting
parsePrimitives: false
is useful when you want all values as strings, like in typical form posts. - Arrays are always returned for duplicate keys, regardless of primitive type.
- All values are
string
unless parsing is enabled (default).
Recommended Use Casesβ
- Parsing URLs or query strings in client-side and server-side environments.
- Reading user input or redirects where the string isnβt available on
window.location.search
. - Back-end parsing of GET query strings for API endpoints.
Conclusion:
parseQueryString
turns any query string into a highly usable JavaScript object, supporting advanced parsing and multi-value keys. It works with any string input, making it ideal for shared or non-browser environments.