How can I read a JSON file in Angular?
Input code in angular
<div class=”form-group row”>
<div class=”col-sm-3"></div>
<label class=”col-sm-2 col-form-label”>Upload mapping Json file </label>
<div class=”col-sm-5">
<input type=”file” class=”form-control” id=”file” (change)=”uploadFile($event)” />
</div>
</div>
In angular side
selectedFile
jsonObj :{} // json object
uploadFile(event) {
this.selectedFile = event.target.files[0];
const fileReader = new FileReader();
fileReader.readAsText(this.selectedFile, “UTF-8”);
fileReader.onload = () => {
//console.log(fileReader.result.toString());
this.jsonObj=(JSON.parse(fileReader.result.toString()));
console.log(this.jsonObj[‘name’])
}
fileReader.onerror = (error) => {
console.log(error);
}
}