Issue
I am working on an angular project. i have gathered data into an array and i can currently download it into a csv format through an angular package but i cannot find a package that will download the same data into a JSON format should my client wish to do so. is there a way to download an array object to a file in JSON format?
Solution
Assuming you are talking about array of objects which you want to download as a josn file then you can go with file-saver.
- install file saver
npm install file-saver --save - for typescript definitions, install
npm install @types/file-saver --save-dev - inside your .ts just import:
import { saveAs } from "file-saver";
Note: no need to import the module in app.module.ts, you can directly import the point no. 3 in your ts wherever required
Finally add below code to download json file
exportTojson() { // exportData is your array which you want to dowanload as json and sample.json is your file name, customize the below lines as per your need. let exportData = this.users; return saveAs( new Blob([JSON.stringify(exportData, null, 2)], { type: 'JSON' }), 'sample.json' );}For more details click here.
Answered By - Anna
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.