
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
json-canonicalize
Advanced tools
JSON canonicalize function
Cryptographic operations like hashing and signing depend on that the target data does not change during serialization, transport, or parsing. By applying the rules defined by JCS (JSON Canonicalization Scheme), data provided in the JSON [RFC8259] format can be exchanged "as is", while still being subject to secure cryptographic operations. JCS achieves this by building on the serialization formats for JSON primitives as defined by ECMAScript [ES6], constraining JSON data to the I-JSON [RFC7493] subset, and through a platform independent property sorting scheme.
The JSON Canonicalization Scheme concept in a nutshell:
JSON.stringify()
Object
properties in a recursive processArray
data is also subject to canonicalization, but element order remains untouchedThis implementation is compatible with JCS / RFC 8785, with a couple of key differences in the default canonicalize
function:
undefined
in arrays: When a value in an array is undefined
, the canonicalize
function treats it as null
. RFC 8785 specifies that it should be treated as undefined
, which can lead to different outputs.To be fully compatible with RFC 8785, you can use the canonicalizeEx
function with the undefinedInArrayToNull
option set to false
:
canonicalizeEx(obj, { undefinedInArrayToNull: false });
yarn add json-canonicalize
Let's demonstrate simple usage with ... example:
import { canonicalize, canonicalizeEx } from 'json-canonicalize';
canonicalize(obj)
// Add `include` and `exclude` options to `canonicalizeEx`.
canonicalizeEx(obj, {exclude:['num', 'dt']})
// add canonicalize to JSON directly.
// which means
// JSON.canonicalize = canonicalize;
import from 'json-canonicalize/src/global';
JSON.canonicalize(obj)
canonicalize(obj, allowCircular)
This is the main function for JSON canonicalization. It takes a JavaScript object and returns its canonical string representation.
obj
(any): The JavaScript object to canonicalize.allowCircular
(boolean, optional): If true
, the function will handle circular references in the object by replacing them with null
. Defaults to false
.canonicalizeEx(obj, options)
This is the extended canonicalization function, offering more granular control over the serialization process.
obj
(any): The JavaScript object to canonicalize.options
(ISerializeOptions, optional): An object with the following properties:
allowCircular
(boolean, optional): Same as in canonicalize
.filterUndefined
(boolean, optional): If true
, undefined
values in objects will be filtered out. Defaults to true
.undefinedInArrayToNull
(boolean, optional): If true
, undefined
values in arrays will be converted to null
. Defaults to true
.include
(string[], optional): An array of property names to include in the canonicalization.exclude
(string[], optional): An array of property names to exclude from the canonicalization.MIT as always
FAQs
JSON canonicalize function
The npm package json-canonicalize receives a total of 13,213 weekly downloads. As such, json-canonicalize popularity was classified as popular.
We found that json-canonicalize demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.