window.location.search query as JSON
Asked Answered
B

13

23

Is there a better way to convert a URL's location.search as an object? Maybe just more efficient or trimmed down? I'm using jQuery, but pure JS can work too.

var query = window.location.search.substring(1), queryPairs = query.split('&'), queryJSON = {};
$.each(queryPairs, function() { queryJSON[this.split('=')[0]] = this.split('=')[1]; });
Blucher answered 30/6, 2011 at 19:1 Comment(0)
L
34

Here's a pure JS function. Parses the search part of the current URL and returns an object. (It's a bit verbose for readability, mind.)

function searchToObject() {
  var pairs = window.location.search.substring(1).split("&"),
    obj = {},
    pair,
    i;

  for ( i in pairs ) {
    if ( pairs[i] === "" ) continue;

    pair = pairs[i].split("=");
    obj[ decodeURIComponent( pair[0] ) ] = decodeURIComponent( pair[1] );
  }

  return obj;
}

On a related note, you're not trying to store the single parameters in "a JSON" but in "an object". ;)

Landside answered 17/8, 2011 at 8:52 Comment(0)
M
37

Please also note that there's an api to query/manipulate search params with: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

var params = new URLSearchParams(window.location.search)
for (let p of params) {
  console.log(p);
}
Mischa answered 18/4, 2019 at 16:45 Comment(1)
Dude, this is the kind of solution I was looking for. Browser support seems to be great except IE. Thanks!Aneroidograph
L
34

Here's a pure JS function. Parses the search part of the current URL and returns an object. (It's a bit verbose for readability, mind.)

function searchToObject() {
  var pairs = window.location.search.substring(1).split("&"),
    obj = {},
    pair,
    i;

  for ( i in pairs ) {
    if ( pairs[i] === "" ) continue;

    pair = pairs[i].split("=");
    obj[ decodeURIComponent( pair[0] ) ] = decodeURIComponent( pair[1] );
  }

  return obj;
}

On a related note, you're not trying to store the single parameters in "a JSON" but in "an object". ;)

Landside answered 17/8, 2011 at 8:52 Comment(0)
L
15

Probably the shortest solution for simple cases:

location.search
  .slice(1)
  .split('&')
  .map(p => p.split('='))
  .reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});
Lodging answered 22/11, 2017 at 21:55 Comment(0)
T
13

If you are using modern browser this produce the same result as accepted answer:

function searchToObject(search) {
  return search.substring(1).split("&").reduce(function(result, value) {
    var parts = value.split('=');
    if (parts[0]) result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
    return result;
  }, {})
}
Taphouse answered 29/12, 2014 at 14:56 Comment(0)
P
5

My approach, simple and clean

var params = "?q=Hello World&c=Awesome";

params = "{\"" + 
         params
         .replace( /\?/gi, "" )
         .replace( /\&/gi, "\",\"" )
         .replace( /\=/gi, "\":\"" ) +
         "\"}";
  
params = JSON.parse( params );

alert( decodeURIComponent( params.q ) );
alert( decodeURIComponent( params.c ) );
Pasahow answered 15/10, 2016 at 16:16 Comment(0)
X
2
Object.fromEntries(new URLSearchParams(location.search))
Xiphoid answered 18/3, 2023 at 8:32 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Hifi
T
1

Just wanted to share this solution using a bit of ESNext and a reducer.

It does pretty much the same suggested by @Carlo but it's a bit cleaner if you're comfortable with ES6 and reducers.

const urlSearchData = searchString => {
    if (!searchString) return false;

    return searchString
        .substring(1)
        .split('&')
        .reduce((result, next) => {
            let pair = next.split('=');
            result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);

            return result;
        }, {});
};

const searchData = urlSearchData(window.location.search);
Tweezers answered 26/4, 2016 at 20:19 Comment(0)
U
1

Building on @rafaelbiten's ES6 work, I added support for params that have no value and query param arrays of the duplicate entry style.

JSFiddle: https://jsfiddle.net/w922xefs/

const queryStringToJSObject = searchString => {
  if (!searchString) return null;

  return searchString
    .replace(/^\?/, '') // Only trim off a single leading interrobang.
    .split('&')
    .reduce((result, next) => {
      if (next === "") {
        return result;
      }
      let pair = next.split('=');
      let key = decodeURIComponent(pair[0]);
      let value = typeof pair[1] !== "undefined" && decodeURIComponent(pair[1]) || undefined;
      if (result.hasOwnProperty(key)) { // Check to see if this property has been met before.
        if (Array.isArray(result[key])) { // Is it already an array?
          result[key].push(value);
        }
        else { // Make it an array.
          result[key] = [result[key], value];
        }
      }
      else { // First time seen, just add it.
        result[key] = value;
      }

      return result;
    }, {}
  );
};

// Simple read of query string
const searchData = queryStringToJSObject(window.location.search);
Unciform answered 10/11, 2017 at 5:18 Comment(0)
V
0

JSON Parse after stringify does the job of converting to a json with array data.

?key1=val1&key2[]=val2.1&key2[]=val2.2&key2[]=val2.3&

{
     'key1' : 'val1',
     'key2' : [ 'val2.1', 'val2.2', 'val2.3' ]
}

function QueryParamsToJSON() {            
    var list = location.search.slice(1).split('&'),
        result = {};

    list.forEach(function(keyval) {
        keyval = keyval.split('=');
        var key = keyval[0];
        if (/\[[0-9]*\]/.test(key) === true) {
            var pkey = key.split(/\[[0-9]*\]/)[0];
            if (typeof result[pkey] === 'undefined') {
                result[pkey] = [];
            }
            result[pkey].push(decodeURIComponent(keyval[1] || ''));
        } else {
            result[key] = decodeURIComponent(keyval[1] || '');
        }
    });

    return JSON.parse(JSON.stringify(result));
}

var query_string = QueryParamsToJSON();
Veld answered 1/4, 2017 at 7:48 Comment(0)
H
0

Note --No doubt above solution works, but it wont cover all the operators

Guess you would want something like this-

var search = location.search;
var trimmedSearch = search.substring(1);

var searchObj = trimmedSearch?JSON.parse(
   '{"' + trimmedSearch.replace(/&/g, '","').replace(/=/g,'":"') + '"}', 
    function(key, value) { 
       return key===""?value:decodeURIComponent(value) 
    }
)
:
{}

console.log(searchObj);

ex -

Override search @1st line with

search = "abc=foo&def=%5Basf%5D&xyz=5&foo=b%3Dar";

Output you get is

Object {abc: "foo", def: "[asf]", xyz: "5", foo: "b=ar"}
Hamhung answered 23/10, 2017 at 18:11 Comment(0)
M
0

In case someone is looking just to access the search query parameters, use this function:

function getQueryStringValue (key)
{
    return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"))
}

Easy to use just call getQueryStringValue(term)

Malachi answered 11/9, 2018 at 23:30 Comment(0)
B
0

I improved @Carlo Zottmann's solution for multiple value for a key. For example you have query like this:

?fruit=apple&fruit=orange&vegetable=parsley

it produces the result:

{ fruit: ['apple', 'orange'], vegetable: 'parsley' }

Solution:

function searchToObject() {
var pairs = window.location.search.substring(1).split("&"),
  obj = {},
  pair,
  i;

for ( i in pairs ) {
  if ( pairs[i] === "" ) continue;

  pair = pairs[i].split("=");

  const objectKey = decodeURIComponent(pair[0]);
  const objectValue = decodeURIComponent(pair[1]);
  if (obj[objectKey]) {
    obj[objectKey] = [
        obj[objectKey],
        objectValue
    ]
  } else {
    obj[objectKey] = objectValue;
  }
}

// refreshing reference
return { ...obj }; }
Bible answered 19/6, 2022 at 9:43 Comment(0)
A
0

you can do this

const searchData = window.location.search.substring(1);
const searchObject = JSON.parse(
    '{"' +
    decodeURI(searchData)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
    '"}'
);
Arturoartus answered 6/9, 2023 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.