Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script type="text/javascript">
    function parseJQueryParams(p) {
        var params = {};
        var pairs = p.split('&');
        for (var i=0; i<pairs.length; i++) {
            var pair = pairs[i].split('=');
            var indices = [];
            var name = decodeURIComponent(pair[0]),
                value = decodeURIComponent(pair[1]);
            var name = name.replace(/\[([^\]]*)\]/g, 
                function(k, idx) { indices.push(idx); return ""; });
            indices.unshift(name);
            var o = params;
            for (var j=0; j<indices.length-1; j++) {
                var idx = indices[j];
                var nextIdx = indices[j+1];
                if (!o[idx]) {
                    if ((nextIdx == "") || (/^[0-9]+$/.test(nextIdx)))
                        o[idx] = [];
                    else
                        o[idx] = {};
                }
                o = o[idx];
            }
            idx = indices[indices.length-1];
            if (idx == "") {
                o.push(value);
            }
            else {
                o[idx] = value;
            }
        }
        return params;
    }
    
    var inEl, outEl;
    function update() {
      outEl.innerHTML = JSON.stringify(parseJQueryParams(inEl.value), null, 2);
    }
    window.addEventListener('load', function() { 
      inEl = document.getElementById('in');
      outEl = document.getElementById('out');
      inEl.addEventListener('keyup', update);
      inEl.addEventListener('change', update);
      update();
    });
  </script>
</head>
<body>
  <h2>Input</h2>
  <p>
  <label for="in">JQuery-style params:</label>
  <input id="in" type="text" value="x[y][1][z][]=1&y=&z=x"/>
  </p>
  <h2>Output</h2>
  <pre id="out" style="border: 1px solid black; padding: 1em;"></pre>
</body>
</html>
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers