This TypeScript utility helps you find differences between two JSON objects with the same schema, preserving the original path structure in the output.
- Compares two JSON objects and identifies differences in values
- Preserves the original JSON path structure in the output
- Works with nested objects and arrays
- Returns only the changed values, making it easy to see what's different
- Includes a CLI tool for comparing JSON files
# Clone the repository
git clone https://github.com/yourusername/json-diff-tool.git
cd json-diff-tool
# Install dependencies
npm install
# Build the project
npm run build
# Install globally (optional)
npm install -g .
# Using npx
npx json-diff original.json modified.json [output.json]
# If installed globally
json-diff original.json modified.json [output.json]
# Using npm start
npm start -- original.json modified.json [output.json]
If the output file is not specified, the differences will be saved as diff_output.json
in the same folder as the original file.
import { findJsonDifferences } from "./jsonDiff";
const original = {
name: "Product",
version: "1.0.0",
settings: {
enabled: true,
timeout: 30,
},
};
const modified = {
name: "Product",
version: "1.0.1",
settings: {
enabled: true,
timeout: 60,
},
};
const differences = findJsonDifferences(original, modified);
console.log(JSON.stringify(differences, null, 2));
Output:
{
"version": "1.0.1",
"settings": {
"timeout": 60
}
}
import { compareJsonFiles } from "./jsonDiff";
async function main() {
try {
const differences = await compareJsonFiles(
"original.json",
"modified.json"
);
console.log(JSON.stringify(differences, null, 2));
} catch (error) {
console.error("Error:", error);
}
}
main();
The project includes comprehensive unit tests for the core JSON difference functionality.
# Run all tests
npm test
# Run tests in watch mode (useful during development)
npm run test:watch
The tests cover:
- Basic primitive value comparisons
- Array comparisons (including nested arrays)
- Object comparisons (including nested objects)
- Complex nested structures
- Edge cases (empty objects/arrays, null/undefined values)
This project is set up as a Git repository. If you've cloned it, you're good to go. If you're setting it up from scratch:
# Make the script executable
chmod +x init-git.sh
# Run the initialization script
./init-git.sh
The script will:
- Initialize a new Git repository
- Add all project files
- Create an initial commit
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
The function recursively traverses both JSON objects and compares values at each level:
- If types don't match, it considers it a complete replacement
- For primitive values, it does a direct comparison
- For objects, it checks each property recursively
- For arrays, it compares elements at the same index
The output contains only the properties that have different values, maintaining the original structure.
- For arrays, if the lengths differ, the entire array is considered different
- The function doesn't detect moved elements in arrays
- It doesn't track deleted properties (only focuses on what's in the modified object)
MIT