0

I have array items, Given below

    writeFile1() {
    this.testnames = [{name : "sheldon", age : "10"}, {name : "Simon", age : "11"}]

    this.file.writeFile(this.file.tempDirectory, './assets/data/demo.json', JSON.stringify(this.testnames))
};

testnames:any;

I need to store "testnames" to the json file which is located at "assets/ data/ demo.json". so I dont know how to do! anyone know this please helpme out.

specification:

Ionic5

Angular 10

1
  • 1
    can you add minimal reproduce steps in stackblitz.com? Commented Nov 10, 2020 at 3:44

1 Answer 1

1

If you are using npm package manager you can do it via file-saver. Just do installation of file-saver package :

npm install file-saver --save

Then import file-saver accordingly:

import { saveAs } from 'file-saver';

const jsonObject: any = [{name : "sheldon", age : "10"}, {name : "Simon", age : "11"}];

const blob = new Blob([JSON.stringify(jsonObject)], {type : 'application/json'});
saveAs(blob, 'demo.json');

Keep in mind that web browser doesnt have the permission to write content to specified location. Instead what you can do is that you can create the File object in the browser iteself and download it accordingly. If it is a web browser it will be downloaded to your default downloading location. I'm providing a Angular based solution here since you have mentioned angular in your question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.