json-extra
json-extra
gives you a little more power to json files and strings
Getting started
npm install --save json-extra
or
yarn add json-extra
Usage
With json-extra
you can do such things like read json files and directly parse them into an object or just create a new json file.
JSON.parse
and JSON.stringify
is also attached to json-extra
.
Include json-extra
as follows:
;
Sync vs. Async
Example:
; // Promises: // Sync:try console catch err console
Methods
isJson()
isJson(json)
Checks if the input is a valid json object
Example:
; ; // true; // false; // false
isJsonString()
isJsonString(jsonString)
Checks if the given string would be a valid json object
Example:
; ; // true; // false; // false
chain()
chain(json[, options])
Options:
- type (array | string): Get specific types. Available options:
array
,object
,string
,boolean
ornumber
- delimiter (string): Changes the delimiter between the chained strings. Default:
'/'
Example:
; var myJsonString = myJson: sub: 'components' 'pages' 'services' ; ;// returns: [ 'myJson', 'myJson/sub', 'myJson/sub/components', 'myJson/sub/pages', 'myJson/sub/services' ];// returns: [ 'myJson/sub' ];// returns: [ 'myJson/sub/components', 'myJson/sub/pages', 'myJson/sub/services' ]
readToObj()
readToObj(path)
Read a json file and returns an obj.
Sync: readToObjSync()
Example:
; // read a json file and return an object ;
write()
write(path, filename[, content])
write a new json file. The content could be a object or a json string.
Sync: writeSync()
Example:
; // writed a new json file ;
find()
find(jsonObject, findString[, options])
Finds a specific key in the json
Options:
- type (array | string): Get specific types. Available options:
array
,object
,string
,boolean
ornumber
- max (boolean): The maximum of keys to find. Default:
-1
alias unlimited
Example:
; const myJsonObejct = const foundKeys = // returns: [ { key: 'dependencies', type: 'object', data: {} } ]