Skip to content

Accept a query string and return a converted javascript object

License

Notifications You must be signed in to change notification settings

neha-saggam/qs-to-json

Repository files navigation

Query String to JSON

Codacy Badge Codacy Badge

Library to parse given decoded query string and create a JSON object.

Installation

Install qs-to-json using npm:

npm install --save-dev qs-to-json

Example

NOTE: : qs-to-json currently does not decode the query string. Make sure you pass decoded queryString.

  1. Convert "&a=1&b=2" to a JSON object:
import queryStringToJSON from "qs-to-json";

const queryString = "&a=1&b=2";
const json = queryStringToJSON(queryString);
console.log(json);

Output would be the following:

{
  "a": "1",
  "b": "2"
}
  1. Convert "&a[]=1&a[]=2" to a JSON object:
import queryStringToJSON from "qs-to-json";

const queryString = "&a[]=1&a[]=2";
const json = queryStringToJSON(queryString);
console.log(json);

Output would be the following:

{
  "a": ["1", "2"]
}
  1. Convert "a.name=Nick&b.name=John" to a JSON object:
import queryStringToJSON from "qs-to-json";

const queryString = "a.name=Nick&b.name=John";
const json = queryStringToJSON(queryString);
console.log(json);

Output would be the following:

{
  "a": { "name": "Nick" },
  "b": { "name": "John" }
}

Playground

Demo

Contributing

We accept pull requests :D

License

qs-to-json is MIT licensed.

About

Accept a query string and return a converted javascript object

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •