1.3.1 • Published 4 years ago

@simonedelpopolo/json-parse v1.3.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 years ago

json-parse

Check if the given string is valid json syntax before to parse it and return an object
javascript ESM module

Other functionalities

Installation

npm install @simonedelpopolo/json-parse

Usage

ESM

parse json from plain string

import { parse } from '@simonedelpopolo/json-parse'

const jsonObject = await parse( '{"goodMorning":"folks"}' )
console.log( jsonObject ) // { goodMorning: 'folks' }

parse json from Buffer.from()

import { parse } from '@simonedelpopolo/json-parse'

// you may load a json file with readFile instead, the below is just a working example

const jsonObject = await parse( Buffer.from('{"goodMorning":"folks"}') )
console.log( jsonObject ) // { goodMorning: 'folks' }

Other Functionalities

  • Function property_value(array)

Given an array it will give back a JSON string or an object

where the first entry of the array will be the property name of the object and the second entry of the array will be the value of the property.
The given array length must be multiple of two.

By default, return a json string

Usage

ESM

return a json string

import { property_value } from '@simonedelpopolo/json-parse'

const jsonString = await property_value( [ '--file',
    './path/to/file',
    '--json',
    'true',
    '-in-object',
    'true'
] )
console.log( jsonString ) // {"--file":"./path/to/file","--json":"true","-in-object":"true"}

return an object

import { property_value } from '@simonedelpopolo/json-parse'

const object = await property_value( [ '--file',
    './path/to/file',
    '--json',
    'true',
    '-in-object',
    'true'
], true ) // set the object parameter for the function to true
console.log( object ) // { '--file': './path/to/file', '--json': 'true', '-in-object': 'true' }
  • Function is_json(string)

Given a string or Buffer it will return a boolean, true if is JSON string false otherwise

Usage

ESM

check if is json from plain string

import { is_json } from '@simonedelpopolo/json-parse'

const isJson = await is_json( '{"goodMorning":"folks"}' )
console.log( isJson ) // true

check if is json from Buffer.from()

import { is_json } from '@simonedelpopolo/json-parse'

// you may load a json file with readFile instead, the below is just a working example

const isJson = await is_json( Buffer.from('good morning folks') )
console.log( isJson ) // false