Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/haoliangyu/rx-to-json
RxJS 5 operator to create JSON file
https://github.com/haoliangyu/rx-to-json
json operator rxjs5 write
Last synced: about 1 month ago
JSON representation
RxJS 5 operator to create JSON file
- Host: GitHub
- URL: https://github.com/haoliangyu/rx-to-json
- Owner: haoliangyu
- License: mit
- Created: 2017-10-23T05:01:54.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-24T04:29:01.000Z (about 7 years ago)
- Last Synced: 2024-04-24T04:11:50.734Z (7 months ago)
- Topics: json, operator, rxjs5, write
- Language: JavaScript
- Size: 41 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# rx-to-json
![build status](https://travis-ci.org/haoliangyu/rx-to-json.svg?branch=master)
[![ReactiveX](http://reactivex.io/assets/Rx_Logo_S.png)](http://reactivex.io/)
[RxJS 5](http://reactivex.io/) operator to write data into a [JSON](https://en.wikipedia.org/wiki/JSON) file
Work in both JavaScript and TypeScript
## Installation
```
npm install rx-to-json
```## Use
Import this library and it will add `toJSON` operator to the rxjs `Observable` class.
```
public toJSON(path: string, options?: any): Observable
```Parameters:
* **path**: JSON file path
* **options**: optional configuration for the JSON creation
* **header**: JSON header. Default: `[`
* **footer**: JSON footer. Default: `]`## Example
Generate a JSON file from data flow:
``` javascript
import { Observable } from 'rxjs';
import 'rx-to-json';let data = [
{ id: 1, name: 'Mike' },
{ id: 2, name: 'Tommy' }
];Observable.of(...data)
.toJSON('data.json')
.subscribe();// output file:
// [{"id":1,"name":"Mike"},{"id":2,"name":"Tommy"}]```
Download data from a PostgreSQL dadtabase and save it as a JSON file:
``` javascript
import pgrx from 'pg-reactive';
import 'rx-to-json';let db = new pgrx('connection string');
db.query('SELECT id, display_name FROM users')
.map((row) => {
// convert the data to match column names
return {
id: row.id,
name: row.display_name
};
})
.toJSON('data.json')
.subscribe();
```## License
MIT