New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-query-string

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-query-string

Converts JSON objects into readable query strings

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61
increased by8.93%
Maintainers
1
Weekly downloads
 
Created
Source

json-query-string

License: MIT npm

Converts JSON objects to/from a minimal query string format.

Advantages over regular query strings

  • Can take arbitrary JSONable objects
  • Preserves "null", "undefined" values
  • Preserves type of values: numbers vs strings vs boolean

Usage

Basic use:

    var queryString = jsonQueryStringify({foo:"bar", baz: 3}); //{foo=%22bar%22&baz=3}, displays in browser as {foo="bar"&baz=3}
    var url = "http://mysite.org?" + queryString;
    //Later...
    jsonQueryParse(queryString); //Original object
    //Non-alphanumeric properties will be quoted:
    jsonQueryStringify({'"&={}':34.5}); //{%22%5C%22%26%3D%7B%7D%22=34.5}, displays in browser as {"%5C"%26%3D%7B%7D"=34.5} - ugly but workable

For an even leaner query string, assuming your query is an object:

    var queryString = jsonQueryStringifyObjectBare({foo:"bar", baz: 3}); //foo=%22bar%22&baz=3, displays in browser as foo="bar"&baz=3
    jsonQueryParseObjectBare(queryString);

For use in React router:

export const routerHistory = useRouterHistory(...)({ parseQueryString: jsonQueryParseObjectBare, stringifyQuery: jsonQueryStringifyObjectBare});

Format

JSON.parse(JSON.stringify(value)) should always equal jsonQueryParse(jsonQueryStringify(value)), with one exception: If an object has undefined values, jsonQueryStringify() preserves their presence. For example, JSON stringify translates {foo:undefined} to {}, while jsonQueryStringify translates to {foo:undefined}.

Keywords

FAQs

Package last updated on 15 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc