npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonify-js

v1.1.2

Published

A simple and light-weight JSON handler with promise support

Downloads

17

Readme

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-js

Usage

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 in object or array format.
  • 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 is null. Refer this for more details.
  • space (optional): You can pass in the space value in number. space = 2 means normal space and space = 4 means tab space. Refer this for more details.
  • fileWriteOptions (optional): Refer this for more details. Default value is set to utf8.
  • customSuccessMessage (optional): You can pass the custom message which will be displayed on console on completion of logging. Default value is set to jsonify-js: Log file has been created!.

Examples

Refer this for more examples

License

Licensed under MIT

Copyright (c) 2021 Syed Afroz Pasha