https://github.com/georgeosddev/jsonkey
Promised json key search
https://github.com/georgeosddev/jsonkey
Last synced: 2 months ago
JSON representation
Promised json key search
- Host: GitHub
- URL: https://github.com/georgeosddev/jsonkey
- Owner: georgeOsdDev
- License: mit
- Created: 2014-12-09T13:24:56.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-03-18T09:42:27.000Z (about 5 years ago)
- Last Synced: 2025-02-20T20:03:28.675Z (3 months ago)
- Language: JavaScript
- Homepage: http://georgeosddev.github.io/jsonkey
- Size: 91.8 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# JSONKey [](https://travis-ci.org/georgeOsdDev/jsonkey) [](http://badge.fury.io/js/jsonkey)
[](https://saucelabs.com/u/georgeosddev_jsonkey)
Promised json key search.
## Usage
### In Browser
Use bower.
```bash
bower isntall jsonkey
``````html
```
`JSONKey` will be installed to global.### In Node.js
Use npm.```bash
npm isntall jsonkey
``````javascript
var JSONKey = require("jsonkey");
```## API
### Constructor
```javascript
var parser = new JSONKey(/*timeout: Number*/);
```
Create a json parser.#### params
* `timeout` :Number :Optional(Default 100)
All promise created by `key` method will be rejected when specified milliseconds was past before key was found.
### key
```javascript
var findingName = parser.key(/*key: String*/);
```Create promise for value corresponding to specified key.
#### params
* `key` :String :Required
You can use dot notation for nested object and brackets for array index.
#### example
```javascript
parser.key("name.last")
.then(function(value){
console.log("LastName is " + value);
});parser.key("favorites[0]")
.then(function(value){
console.log("Fist favorite is " + value);
})
parser.key("address")
.then(function(value){
console.log(value);
},function(){
console.log("address is not found");
})var jsonString = JSON.stringify({
name:{
last:"aaa",
first:"bbb"
},
age:50,
favorites:["book","beer"]
});
parser.parse(jsonString);
// "LastName is aaa"
// "Fist favorite is book"
// "address is not found"
```### filter, pipe, recovery, map, reduce, etc
`promise` generated by `key` function is chain-able.
So easily create function chain as functional programming style.
See also APIs of [Promisechain](https://github.com/georgeOsdDev/promisechain).```javascript
parser.key("items")
.map(function(item){
return item.price;
})
.reduce(function(acc, price){
return acc + price;
}, 0)
.pipe(function(val){
return "TotalPrice is:" + val;
})
.then(function(result){
console.log(result);
});var jsonString = JSON.stringify({
items:[
{name:"book", price:10},
{name:"apple", price:3},
{name:"banana", price:2},
{name:"water", price:1},
]
});
parser.parse(jsonString);
// => "TotalPrice is: 16"```
### on, once, addListener, etc
`JSONKey` inherits Node.js's build in `EventEmitter`.
So parser works as `EventEmitter` itself.
Each key in jsonString will be emitted as event.#### example
```javascript
var parser = new JSONKey();
var parser.on("age", function(age){
console.log("He is " + age + "years old");
});var jsonString = JSON.stringify({
name:{
last:"aaa",
first:"bbb"
},
age:50,
favorites:["book","beer"]
});
parser.parse(jsonString);
```## Development
Install Node.js and NPM.
```bash
git clone git://github.com/georegeosddev/jsonkey.git
cd jsonkey
npm install
npm run-script build
```## Licence
MIT