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-sim

v1.0.0

Published

Compute the similarity score between two JSON objects

Downloads

75

Readme

JSON Sim

A Node.js module to compute the similarity score between two JSON objects, outputting a score between 0 and 1. The comparison is recursive, case-insensitive for strings, and order-insensitive for arrays.

Features

  • Recursive Comparison: Deeply compares nested JSON objects and arrays.
  • Case-Insensitive Strings: Strings are converted to lowercase before comparison.
  • Order-Insensitive Arrays: Arrays are treated as sets; the order of elements doesn't affect the similarity score.
  • No dependencies: No additional dependencies needed.

Installation

Install the package via npm:

npm install json-sim

Usage

const {
  jsonSimilarity,
  jsonSimilarityPerKey,
  batchJsonSimilarityPerKey,
  batchJsonSimilarity,
} = require("json-sim");

Compute Similarity Between Two Objects

const obj1 = {
  name: "John",
  age: 30,
  hobbies: ["Reading", "Swimming"],
};

const obj2 = {
  name: "john",
  age: 30,
  hobbies: ["swimming", "reading"],
};

const similarityScore = jsonSimilarity(obj1, obj2);
console.log(`Similarity Score: ${similarityScore}`); // Output: Similarity Score: 1

Compute Similarity Score Per Key

const targetObj = { name: "Alice", age: 25 };
const testObj = { name: "alice", age: 24 };

const similarityPerKey = jsonSimilarityPerKey(targetObj, testObj);
console.log("Similarity Per Key:", similarityPerKey);
// Output: { name: 1, age: 0 }

Compute Batch Similarity Score Per Key

const targetList = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
];
const testList = [
  { name: "alice", age: 25 },
  { name: "bob", age: 31 },
];

const batchSimilarityPerKey = batchJsonSimilarityPerKey(targetList, testList);
console.log("Batch Similarity Per Key:", batchSimilarityPerKey);
// Output: { name: 1, age: 0.75 }

Compute Batch Similarity

const targetList = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
];
const testList = [
  { name: "alice", age: 25 },
  { name: "bob", age: 31 },
];

const batchSimilarity = batchJsonSimilarity(targetList, testList);
console.log("Batch Similarity:", batchSimilarity);
// Output: 0.916...

Command-Line Usage

After installing the package globally, you can use the json-sim command:

npm install -g json-sim

json-sim file1.json file2.json

Using npx

Alternatively, you can use npx to run the command without installing it globally:

npx json-sim file1.json file2.json

API

jsonSimilarity(obj1, obj2)

Computes the similarity score between two JSON objects.

  • Parameters:

    • obj1 (Object): The first JSON object.
    • obj2 (Object): The second JSON object.
  • Returns:

    • (Number): A similarity score between 0 and 1.

How It Works

  • Primitive Types: Compares numbers and booleans directly. For strings, it compares them in lowercase to ensure case insensitivity.
  • Arrays: Finds the best match for each element in one array with the elements in the other array, summing up the maximum similarities.
  • Objects: Collects all keys from both objects and recursively computes the similarity for each key that exists in both objects.

jsonSimilarityPerKey(targetObj, testObj)

Calculates the similarity score for each key between a target object and its test pair.

  • Parameters:

    • targetObj (Object): The target JSON object.
    • testObj (Object): The test JSON object to compare with the target.
  • Returns:

    • (Object): An object mapping each key to its similarity score.

batchJsonSimilarityPerKey(targetList, testList)

Computes the average similarity score per key over lists of target and test objects.

  • Parameters:

    • targetList (Array<Object>): List of target JSON objects.
    • testList (Array<Object>): List of test JSON objects.
  • Returns:

    • (Object): An object mapping each key to its average similarity score.

batchJsonSimilarity(targetList, testList)

Computes the average similarity score between two lists of JSON objects.

  • Parameters:

    • targetList (Array<Object>): List of target JSON objects.
    • testList (Array<Object>): List of test JSON objects.
  • Returns:

    • (Number): The average similarity score between the lists.

Examples

Comparing Nested Objects

const obj1 = {
  user: {
    name: "Alice",
    details: {
      email: "[email protected]",
      preferences: ["News", "Updates"],
    },
  },
};

const obj2 = {
  user: {
    name: "alice",
    details: {
      email: "[email protected]",
      preferences: ["updates", "news"],
    },
  },
};

const similarityScore = jsonSimilarity(obj1, obj2);
console.log(`Similarity Score: ${similarityScore}`); // Output: Similarity Score: 1

Comparing Arrays with Different Lengths

const arr1 = ["Apple", "Banana", "Cherry"];
const arr2 = ["banana", "apple"];

const similarityScore = jsonSimilarity(arr1, arr2);
console.log(`Similarity Score: ${similarityScore}`); // Output: Similarity Score: 0.666...

Contributing

Contributions are welcome! Please submit an issue or pull request on the GitHub repository.


Feel free to integrate this package into your project. For any issues or feature requests, please open an issue on GitHub.