quick-jsonify
v1.2.0
Published
A versatile JSON utility package with features like jsonify, xmlToJson, masking, validation, flattening, transformation, and more.
Downloads
46
Maintainers
Readme
Quick-JSONify
A simple yet powerful JSON utility package for Node.js, providing features like JSON formatting, xml To json, key transformation, validation, masking, and more.
Installation
npm install quick-jsonify
Features
- jsonify - Convert an object to a JSON string with customizable options.
- getSummary - Get a summary of the JSON data structure.
- validate - Validate JSON data against a schema.
- sortObjectKeys - Sort the keys of a JSON object.
- flattenObject - Flatten a nested JSON object.
- unflattenObject - Unflatten a flattened JSON object.
- deepClone - Create a deep clone of a JSON object.
- prettyPrint - Print a JSON object in a formatted way with optional highlights.
- transformValues - Transform values in a JSON object using a custom function.
- transformKeys - Transform the keys of a JSON object (e.g., camelCase).
- mergeJsonObjects - Merge multiple JSON objects into one.
- filterJsonKeys - Filter a JSON object to include only specified keys.
- generateSampleJson - Generate a sample JSON object with specified keys.
- jsonToXml - Convert a JSON object to an XML string.
- deepFreeze - Make a JSON object immutable.
- maskSensitiveData - Mask sensitive data in a JSON object.
- addCommentsToJson - Add comments to a JSON object.
Usage/Example
1. jsonify
Convert an object to a JSON string with customizable options.
const { jsonify } = require('quick-jsonify');
const data = { name: "John", age: 30, city: "New York" };
const options = { indent: 2, highlight: true };
console.log(jsonify(data, options));
2. getSummary
Get a summary of the JSON data structure.
const { getSummary } = require('quick-jsonify');
const data = { name: "John", age: 30, city: "New York" };
console.log(getSummary(data));
// Output: { type: 'object', properties: 3 }
3. validate
Validate JSON data against a schema.
const { validate } = require('quick-jsonify');
const data = { name: "John", age: "30" };
const schema = { name: "string", age: "number" };
console.log(validate(data, schema));
// Output: { valid: false, errors: [{ key: 'age', expectedType: 'number', actualType: 'string' }] }
4. sortObjectKeys
Sort the keys of a JSON object.
const { sortObjectKeys } = require('quick-jsonify');
const data = { b: 2, a: 1, c: 3 };
console.log(sortObjectKeys(data));
// Output: { a: 1, b: 2, c: 3 }
5. flattenObject
Flatten a nested JSON object.
const { flattenObject } = require('quick-jsonify');
const data = { a: { b: { c: 1 } } };
console.log(flattenObject(data));
// Output: { 'a.b.c': 1 }
6. unflattenObject
Unflatten a flattened JSON object.
const { unflattenObject } = require('quick-jsonify');
const data = { 'a.b.c': 1 };
console.log(unflattenObject(data));
// Output: { a: { b: { c: 1 } } }
7. deepClone
Create a deep clone of a JSON object.
const { deepClone } = require('quick-jsonify');
const data = { name: "John", age: 30 };
const clone = deepClone(data);
clone.name = "Jane";
console.log(data.name); // Output: John
console.log(clone.name); // Output: Jane
8. prettyPrint
Print a JSON object in a formatted way with optional highlights.
const { prettyPrint } = require('quick-jsonify');
const data = { name: "John", age: 30 };
prettyPrint(data);
9. transformValues
Transform values in a JSON object using a custom function.
const { transformValues } = require('quick-jsonify');
const data = { name: "John", age: 30 };
const transformed = transformValues(data, value => (typeof value === "number" ? value * 2 : value));
console.log(transformed); // Output: { name: "John", age: 60 }
10. transformKeys
Transform the keys of a JSON object (e.g., to camelCase).
const { transformKeys } = require('quick-jsonify');
const data = { "first_name": "John", "last_name": "Doe" };
const transformed = transformKeys(data, "camelCase");
console.log(transformed); // Output: { firstName: "John", lastName: "Doe" }
11. mergeJsonObjects
Merge multiple JSON objects into one.
const { mergeJsonObjects } = require('quick-jsonify');
const obj1 = { a: 1 };
const obj2 = { b: 2 };
console.log(mergeJsonObjects(obj1, obj2));
// Output: { a: 1, b: 2 }
12. filterJsonKeys
Filter a JSON object to include only specified keys.
const { filterJsonKeys } = require('quick-jsonify');
const data = { name: "John", age: 30, city: "New York" };
console.log(filterJsonKeys(data, ["name", "city"]));
// Output: { name: "John", city: "New York" }
13. generateSampleJson
Generate a sample JSON object with specified keys.
const { generateSampleJson } = require('quick-jsonify');
console.log(generateSampleJson(["name", "age"], "default"));
// Output: { name: "default", age: "default" }
14. jsonToXml
Convert a JSON object to an XML string.
const { jsonToXml } = require('quick-jsonify');
const data = { name: "John", age: 30 };
console.log(jsonToXml(data));
// Output: <name>John</name><age>30</age>
15. deepFreeze
Make a JSON object immutable.
const { deepFreeze } = require('quick-jsonify');
const data = { name: "John", age: 30 };
deepFreeze(data);
data.age = 40; // This won't change the value
console.log(data.age); // Output: 30
16. maskSensitiveData
Mask sensitive data in a JSON object.
const { maskSensitiveData } = require('quick-jsonify');
const data = { name: "John", password: "secret" };
console.log(maskSensitiveData(data, ["password"]));
// Output: { name: "John", password: "***" }
17. addCommentsToJson
Add comments to a JSON object.
const { addCommentsToJson } = require('quick-jsonify');
const data = { name: "John", age: 30 };
const comments = { age: "Age in years" };
console.log(addCommentsToJson(data, comments, 2));
// Output:
// {
// "name": "John",
// "age": 30 // Age in years
// }
License
This package is open-source and licensed under the MIT License.