npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-order

v1.1.3

Published

Control the order of properties in JSON via a lookup object - including nested properties.

Downloads

2,806

Readme

📚 json-order

npm Azure DevOps builds

json-order allows for conversion between JS Objects and a JSON string while keeping property order, controlled via a property map. All manner of nesting is supported.

Usage

parse

To parse a JSON string and generate a map:

const result = orderedJson.parse(srcJson, prefix, separator);

Parameters

  • srcJson: a json string
  • prefix [optional]: a non-empty string that controls what the key prefix value is in the generated map. Defaults to $.
  • separator [optional]: a non-empty string that controls what the key separator is in the generated map. Defaults to ~.

result.object will contain a JS object while result.map will contain the generated property map.

stringify

To stringify a JS object maintaining a particular property order:

const jsonString = orderedJson.stringify(srcObj, map, 2);

Parameters

  • srcObj: an object with the properties in any order
  • map [optional]: the property map generated by parse above. If the map is unset, the response is a standard JSON.stringify.
  • separator [optional]: a non-empty string that controls what the key separator is in the generated map. Defaults to ~.
  • space [optional]: a number used to insert white space into the output JSON string for readability purposes, as per the JSON.stringify documentation.

order

To duplicate a JS object but containing a particular property order:

const orderedObj = orderedJson.order(srcObj, map, 2);

Parameters

  • srcObj: an object with the properties in any order
  • map [optional]: the property map generated by parse above. If the map is unset, the response is a standard JSON.stringify.
  • separator [optional]: a non-empty string that controls what the key separator is in the generated map. Defaults to ~.

Example

An object with a particular property order:

{
  "z": {
    "y": 5,
    "b": [
      false,
      {
        "d": 3,
        "c": 2
      },
      14
    ]
  },
  "a": "hello"
}

Will generate a lookup object like:

{
  "$": ["z", "a"],
  "$.z": ["y", "b"],
  "$.b.1": ["d", "c"]
}

Why?

JS Objects in JavaScript do keep their property insertion order (this behaviour is dependant on the JS engine), however this behaviour is not guaranteed by other systems you may interchange that object with.

For example, when storing a JS Object in a SQLite database, the returned object will have its properties in alphabetical order. Order in an array is preserved, however order of properties in an object is not.

This behavior is undesirable in certain cases. If a user has configured an application via JSON, they may choose to make logical groupings of properties. When this JSON is parsed to a JS Object, stored in the DB and extracted again, the groupings no longer exist because the properties have been alphabetically ordered.

There are several solutions to this problem, (eg. storing the data in a different format) but this migration process can be tedious and complex in certain use cases.

This particular implementation is in reference to the approach suggested for feature 1046 in Insomnia.

Contributing

Please raise issues or feature requests via the Github issue tracker

Feel free to submit a pull request with your change!

yarn install
yarn test

License

MIT