Skip to content

matthewwithanm/query-string

Repository files navigation

query-string Build Status

Parse and stringify URL query strings

Install

Download manually or with a package-manager.

npm install --save query-string
bower install --save query-string
component install sindresorhus/query-string

Examples

Node.js

var queryString = require('query-string');

var url = 'http://sindresorhus.com?foo=bar'.split('?');
var parsed = queryString.parse(url[1]);
console.log(parsed);
// {foo: 'bar'}

parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
console.log(url[0] + '?' + queryString.stringify(parsed));
// http://sindresorhus.com?foo=unicorn&ilike=pizza

Bower

<script src="bower_components/query-string/query-string.js"></script>
console.log(location.search);
// ?foo=bar

var parsed = queryString.parse(location.search);
console.log(parsed);
// {foo: 'bar'}

parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
location.search = queryString.stringify(parsed);

console.log(location.search);
// ?foo=unicorn&ilike=pizza

API

queryString.parse(string)

Parse a query string into an object.

queryString.stringify(object)

Stringify an object into a query string.

Nesting

This module intentionally doesn't support nesting as it's not specced and varies between implementations, which causes a lot of edge cases.

You're much off just converting the object to a JSON string:

queryString.stringify({
  foo: 'bar',
  nested: JSON.stringify({
    unicorn: 'cake'
  })
});
// foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D

License

MIT © Sindre Sorhus

About

Parse and stringify URL query strings

Resources

Stars

Watchers

Forks

Packages

No packages published