ngs-json-utils
is a lightweight utility library for Angular applications that provides easy-to-use functions for working with JSON objects. It includes methods for deep cloning, serialization, and deserialization of JSON data, designed specifically for Angular projects with TypeScript support.
- JSON Serialization & Parsing: Convert between objects and JSON with default values.
- Deep Object Manipulation: Copy, merge, and update nested objects.
- Object Comparison: Shallow and deep object equality checks.
- Key Filtering: Filter objects by specified keys.
- Map <-> JSON Conversion: Convert between JSON objects and
Map
. - Safe Key Search: Safe key lookup in objects.
- Validation: Validate JSON strings for correctness.
- Install
ngs-json-utils
via npm:
npm install ngs-json-utils
- Import NgsJsonUtilsService into your Angular component or service:
import { NgsJsonUtilsService } from "ngs-json-utils";
- Inject the service into your component by using dependency injection or the inject function:
export class MyComonent {
constructor(private ngsJsonUtilsService: NgsJsonUtilsService) {}
// or
ngsJsonUtilsService = inject(NgsJsonUtilsService);
}
- Converts an object into a JSON string.
const result = this.ngsJsonUtilsService.stringify({ name: "John", age: 30 });
console.log(result); // {"name":"John","age":30}
- Converts a JSON string into a JavaScript object.
const json = '{"name":"John","age":30}';
const obj = this.ngsJsonUtilsService.parse(json, { name: "Default", age: 0 });
console.log(obj); // { name: "John", age: 30 }
- Creates a deep copy of an object.
const original = { name: "John", details: { age: 30 } };
const copy = this.ngsJsonUtilsService.deepCopy(original);
console.log(copy); // { name: 'John', details: { age: 30 } }
- Checks if a string is a valid JSON.
const isValid = this.ngsJsonUtilsService.isValidJSON('{"name":"John"}');
console.log(isValid); // true
- Compares two objects based on their serialized representations.
const obj1 = { name: "John" };
const obj2 = { name: "John" };
const areEqual = this.ngsJsonUtilsService.equalJSON(obj1, obj2);
console.log(areEqual); // true
- Performs a deep comparison of two objects.
const obj1 = { name: "John", details: { age: 30 } };
const obj2 = { name: "John", details: { age: 30 } };
const areEqualDeep = this.ngsJsonUtilsService.deepEqualJSON(obj1, obj2);
console.log(areEqualDeep); // true
- Performs a deep merge of two objects.
const target = { name: "John", details: { age: 30 } };
const source = { details: { age: 31 }, address: "New York" };
const merged = this.ngsJsonUtilsService.deepMerge(target, source);
console.log(merged); // { name: 'John', details: { age: 31 }, address: 'New York' }
- Filters an object, keeping only specified keys.
const obj = { name: "John", age: 30, country: "USA" };
const filtered = this.ngsJsonUtilsService.filterKeys(obj, ["name", "country"]);
console.log(filtered); // { name: 'John', country: 'USA' }
- Deeply updates an object based on another.
const target = { name: "John", details: { age: 30 } };
const updates = { details: { age: 31 }, country: "USA" };
const updated = this.ngsJsonUtilsService.deepUpdate(target, updates);
console.log(updated); // { name: 'John', details: { age: 31 }, country: 'USA' }
- Finds a value by key in an object or nested object.
const obj = { name: "John", details: { age: 30 } };
const value = this.ngsJsonUtilsService.findValueByKey(obj, "age");
console.log(value); // 30
- Safely finds a value by key, returning undefined in case of an error.
const obj = { name: "John", details: { age: 30 } };
const value = this.ngsJsonUtilsService.safeFindValueByKey(obj, "age");
console.log(value); // 30
- Removes all undefined values from an object.
const obj = { name: "John", age: undefined, country: "USA" };
const cleaned = this.ngsJsonUtilsService.removeUndefined(obj);
console.log(cleaned); // { name: 'John', country: 'USA' }
- Converts a Map into a regular JSON object.
const map = new Map();
map.set("name", "John");
map.set("age", 30);
const json = this.ngsJsonUtilsService.mapToJSON(map);
console.log(json); // { name: 'John', age: 30 }
- Converts a regular JSON object into a Map.
const json = { name: "John", age: 30 };
const map = this.ngsJsonUtilsService.jsonToMap(json);
console.log(map); // Map { 'name' => 'John', 'age' => 30 }
- Merges multiple arrays of objects, keeping only unique objects based on a key.
const array1 = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
];
const array2 = [
{ id: 2, name: "Jane" },
{ id: 3, name: "Jim" },
];
const uniqueObjects = this.ngsJsonUtilsService.mergeUniqueObjects(array1, array2, "id");
console.log(uniqueObjects); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Jim' }]
To contribute or use the library in development mode, you can clone the repository and install dependencies.
- Fork the repository
- Clone the repo, install dependencies
git clone https://github.com/andrei-shpileuski/ngx-json-utils.git
cd ngx-json-utils
npm install
- Create a new branch for your changes
- Submit a pull request with a detailed description of the changes
angular, json, utils, deep copy, cloning, serialization, deserialization, angular library, typescript, angular utils, ng, ngx, json utilities, json manipulation, deep merge, object comparison, angular helpers