JSPM

quick-jsonify

1.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 3
    • Score
      100M100P100Q39855F
    • License MIT

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

    Package Exports

    • quick-jsonify
    • quick-jsonify/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (quick-jsonify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    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

    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.

    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.