jsonify-js v1.1.2
jsonify-js - JSON with Promise Support
A simple and light-weight JSON handler with promise support. It provide promised version of the JSON built-in methods such as JSON.Stringify and JSON.parse. It also had an utility method (JSONLogger) to log the JSON data on to a JSON file.
Installation
npm install jsonify-jsUsage
You can use the JSONStringify and JSONParse methods using Promises or Async/Await syntax.
Using Promise Syntax
JSONStringify- Converts the js object/array into stringified JSON.const { JSONStringify } = require('jsonify-js') const user = { name: 'John', age: 30, city: 'New York', } JSONStringify(user) .then((result) => { console.log(result) // Handle the stringified result }) .catch((error) => { // Handler error console.error(error) })JSONParse- Converts the stringified JSON into js object/array.const { JSONParse } = require('jsonify-js') const stringifiedData = '{"name":"John","age":30,"city":"New York"}' JSONParse(stringifiedData) .then((result) => { // Handle the JSON parse result console.log(result) }) .catch((error) => { // Handler error console.error(error) })
Using Async/Await Syntax
JSONStringify- Converts the js object into stringified JSON.const { JSONStringify } = require('jsonify-js') const getStringifiedData = async () => { try { const user = { name: 'John', age: 30, city: 'New York', } const stringifiedData = await JSONStringify(user) return stringifiedData } catch (err) { // Handler error console.error(err) } } const main = async () => { const result = await getStringifiedData() console.log('Result:', result) } main()JSONParse- Converts the stringified JSON into js object.const { JSONParse } = require('jsonify-js') const getParsedData = async () => { try { const stringifiedData = '{"name":"John","age":30,"city":"New York"}' const parsedData = await JSONParse(stringifiedData) return parsedData } catch (err) { // Handler error console.error(err) } } const main = async () => { const result = await getParsedData() console.log('Result:', result) } main()
JSON logger
JSONLogger- Can be used to log the JSON data to a JSON file.const { JSONLogger } = require('jsonify-js') const user = { name: 'John', age: 30, city: 'New York', } // Save the stringified data in a JSON file JSONLogger({ data: user, space: 2, filePath = './log.json' })
Documentation
JSONStringify
JSONStringify works similar to the build-in JSON method JSON.stringify. You can also pass the optional parameters such as replacer or space.
Syntax
JSONStringify(value)
JSONStringify(value, replacer)
JSONStringify(value, replacer, space)Read more about the optional parameters here
JSONParse
JSONParse works similar to the build-in JSON method JSON.parse. You can also pass the optional parameters reviver.
Syntax
JSONParse(text)
JSONParse(text, reviver)Read more about the optional parameters here
JSONLogger
JSONLogger can be used to log the JSON data into a JSON file.
Syntax
JSONLogger({
data,
filePath,
replacer,
space,
writeFileOption,
customSuccessMessage,
})Parameters:
data(required): Data inobjectorarrayformat.filePath(optional): Location/Path of the JSON file where you want to log/save the data. Default value is set to./log.json.replacer(optional): You can use the custom replacer function. Default value isnull. Refer this for more details.space(optional): You can pass in the space value in number.space = 2means normal space andspace = 4means tab space. Refer this for more details.fileWriteOptions(optional): Refer this for more details. Default value is set toutf8.customSuccessMessage(optional): You can pass the custom message which will be displayed on console on completion of logging. Default value is set tojsonify-js: Log file has been created!.
Examples
Refer this for more examples
License
Licensed under MIT
Copyright (c) 2021 Syed Afroz Pasha