Query String to JSON (and JSON to Query String)
To convert a query string to JSON in JavaScript, pass it to URLSearchParams and build an object from the pairs: Object.fromEntries(new URLSearchParams(query)). That one line is right for simple strings, but it keeps only the last value of a repeated key and it never builds nested objects. This guide shows the one-liner, the fixes for repeated keys and bracket notation, the Python equivalent, and the reverse direction from JSON back to a query string.
The one-liner
A leading ? is ignored, so location.search can go straight in. For a full URL, use new URL(href).searchParams instead of slicing the string yourself. URLSearchParams also decodes percent-encoding and turns + into a space.
Note that page came back as the string "2". A query string has no types. Everything is text, and converting "2" to a number or "true" to a boolean is your job.
Repeated keys
Many APIs send lists by repeating the key: tag=api&tag=cli. Object.fromEntries silently drops all but the last one:
Collect them into an array instead:
If you only need one key, params.getAll('tag') returns every value for it as an array.
Nested objects and bracket notation
PHP, Rails and many Node back ends read bracket notation like user[name]=Ada&user[roles][]=admin as nested data. URLSearchParams does not. It treats user[name] as a plain key with brackets in its name. For nesting, use the qs package from npm:
Two qs defaults are worth knowing, both from its README. It only nests 5 levels deep, and anything deeper stays as a literal key. It also caps array indexes at 20: a[100]=b becomes the object {"a":{"100":"b"}}, not an array with 100 empty slots. Both limits exist to stop abusive input, and both are options you can change.
Query string to JSON in Python
urllib.parse.parse_qs is in the standard library. It always returns lists, because any key might repeat:
sort is missing because blank values are dropped by default. Pass keep_blank_values=True to keep it. To unwrap single values the way most people expect:
JSON to query string
The reverse is a one-liner too, as long as the object is flat:
Arrays and nested objects are where it breaks. URLSearchParams turns every value into a string, so an array becomes a comma-joined value and an object becomes the text [object Object]:
For repeated keys, append each item:
For bracket notation, qs.stringify does the inverse of qs.parse. Pass encode: false if you want to read the output:
qs writes indexes by default. Its arrayFormat option switches to brackets (a[]=b), repeat (a=b&a=c) or comma (a=b,c), so match whatever the server expects.
In Python, urlencode with doseq=True writes lists as repeated keys:
Passing a whole JSON object in one parameter
Sometimes the simplest contract is one parameter holding JSON. Stringify it and percent-encode it:
This keeps types and nesting intact, at the cost of an unreadable URL. Watch the length: browsers, proxies and servers all cap URL size, and the limits differ. If the filter can grow, send it as a JSON request body instead. To read an encoded value by hand, paste the decoded JSON into the JSON formatter, or use the JSON to String tool to see it escaped.
Common mistakes
- Encoding
+wrong.URLSearchParamsreads+as a space. A literal plus sign must be sent as%2B. This bites Base64 values most often, since Base64 uses+. - Parsing with one library and validating with another. The
qsREADME warns thatURLSearchParamsandqscan read the same string differently, so a check run on one result does not protect the other. Parse once and check the object you actually use. - Assuming types.
page=2is"2"andactive=falseis the string"false", which is truthy in JavaScript. Convert, or validate with a schema. The JSON to Zod tool generates a schema from a sample object, andz.coerce.number()handles the string-to-number step.
FAQ
How do I convert a query string to JSON in JavaScript?
Use Object.fromEntries(new URLSearchParams(query)) for a flat query string, then JSON.stringify the result. If keys repeat, loop over the params and collect values into arrays. For bracket notation like user[name]=Ada, use qs.parse.
How do I convert a JSON object to a query string?
For a flat object, new URLSearchParams(obj).toString(). For arrays, append each item under the same key. For nested objects, use qs.stringify, or send the object as one percent-encoded JSON parameter.
Can a query string contain JSON?
Yes, as long as it is percent-encoded with encodeURIComponent. The server reads the parameter as text and parses it with its JSON parser.