UNPKG

quick-jsonify

Version:

A versatile JSON utility package with features like jsonify, xmlToJson, masking, validation, flattening, transformation, and more.

218 lines (171 loc) 6.45 kB
# 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** ```bash npm install quick-jsonify ``` ## Features 1. **jsonify** - Convert an object to a JSON string with customizable options. 2. **getSummary** - Get a summary of the JSON data structure. 3. **validate** - Validate JSON data against a schema. 4. **sortObjectKeys** - Sort the keys of a JSON object. 5. **flattenObject** - Flatten a nested JSON object. 6. **unflattenObject** - Unflatten a flattened JSON object. 7. **deepClone** - Create a deep clone of a JSON object. 8. **prettyPrint** - Print a JSON object in a formatted way with optional highlights. 9. **transformValues** - Transform values in a JSON object using a custom function. 10. **transformKeys** - Transform the keys of a JSON object (e.g., camelCase). 11. **mergeJsonObjects** - Merge multiple JSON objects into one. 12. **filterJsonKeys** - Filter a JSON object to include only specified keys. 13. **generateSampleJson** - Generate a sample JSON object with specified keys. 14. **jsonToXml** - Convert a JSON object to an XML string. 15. **deepFreeze** - Make a JSON object immutable. 16. **maskSensitiveData** - Mask sensitive data in a JSON object. 17. **addCommentsToJson** - Add comments to a JSON object. ## Usage/Example **1. jsonify**\ Convert an object to a JSON string with customizable options. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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). ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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. ```javascript 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.