json-stable-stringify vs sort-json
JSON操作ライブラリ
json-stable-stringifysort-json類似パッケージ:

JSON操作ライブラリ

JSON操作ライブラリは、JavaScriptオブジェクトをJSON形式に変換するためのツールであり、データの整列や安定した文字列化を提供します。これにより、データの一貫性と可読性が向上し、特にAPI通信やデータ保存の際に重要です。これらのライブラリは、異なるシナリオにおいてJSONデータを扱う際の利便性を向上させます。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
json-stable-stringify10,819,9187836.4 kB710ヶ月前MIT
sort-json523,6417322.5 kB15-MIT

機能比較: json-stable-stringify vs sort-json

安定した文字列化

  • json-stable-stringify:

    json-stable-stringifyは、オブジェクトを安定した順序で文字列化します。これにより、同じオブジェクトを何度も文字列化しても、常に同じ結果が得られます。これは、オブジェクトのプロパティの順序が異なる場合でも、比較やハッシュ化が可能になるため、特に重要です。

  • sort-json:

    sort-jsonは、オブジェクト内のプロパティを指定したキーに基づいてソートしますが、安定した順序での文字列化は提供しません。したがって、出力の順序が毎回異なる可能性があります。

選び方: json-stable-stringify vs sort-json

  • json-stable-stringify:

    json-stable-stringifyは、オブジェクトを安定した順序で文字列化する必要がある場合に選択してください。特に、オブジェクトのプロパティの順序が重要な場合や、同じオブジェクトを何度も文字列化して比較する必要がある場合に適しています。

  • sort-json:

    sort-jsonは、JSONオブジェクトを簡単にソートしたい場合に選択してください。特定のキーに基づいてオブジェクトをソートし、出力を整然とした形式にすることができます。特に、データの視覚化や比較を行う際に便利です。

json-stable-stringify のREADME

json-stable-stringify Version Badge

github actions coverage License Downloads

npm badge

deterministic version of JSON.stringify() so you can get a consistent hash from stringified results

You can also pass in a custom comparison function.

example

const stringify = require('json-stable-stringify');

const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };

console.log(stringify(obj));

output:

{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

methods

const stringify = require('json-stable-stringify')

const str = stringify(obj, opts)

Return a deterministic stringified string str from the object obj.

options

cmp

If opts is given, you can supply an opts.cmp to have a custom comparison function for object keys. Your function opts.cmp is called with these parameters:

opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue }, { get(key): value })

For example, to sort on the object key names in reverse order you could write:

const stringify = require('json-stable-stringify');

const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 };

const s = stringify(obj, function (a, b) {
	return b.key.localeCompare(a.key);
});

console.log(s);

which results in the output string:

{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}

Or if you wanted to sort on the object values in reverse order, you could write:

const stringify = require('json-stable-stringify');

const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };

const s = stringify(obj, function (a, b) {
	return a.value < b.value ? 1 : -1;
});

console.log(s);

which outputs:

{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}

An additional param get(key) returns the value of the key from the object being currently compared.

space

If you specify opts.space, it will indent the output for pretty-printing. Valid values are strings (e.g. {space: \t}) or a number of spaces ({space: 3}).

For example:

const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };

const s = stringify(obj, { space: '  ' });

console.log(s);

which outputs:

{
  "a": {
    "and": [
      1,
      2,
      3
    ],
    "foo": "bar"
  },
  "b": 1
}

replacer

The replacer parameter is a function opts.replacer(key, value) that behaves the same as the replacer from the core JSON object.

install

With npm do:

npm install json-stable-stringify

license

MIT