How to Raed data from JSON file in synchronous mode using Angular 15
How to Raed data from JSON file in synchronous mode using Angular 15
Know the answer? Post it. Somebody with the same question will find it here.

In Angular 15, reading data from a JSON file synchronously is not recommended as it can block the main thread and negatively impact the performance of your application. However, if you still need to read data from a JSON file synchronously, you can achieve this by using the HttpClient module and the toPromise method.
Here's an example of how you can read data from a JSON file synchronously in Angular 15:
HttpClient module in your component:import { HttpClient } from '@angular/common/http';HttpClient module in your component's constructor:constructor(private http: HttpClient) { }readJSONFile(): any {
const fileUrl = 'path/to/your/json/file.json';
return this.http.get(fileUrl).toPromise();
}readJSONFile method in your component's code:async getDataFromJSON() {
try {
const jsonData = await this.readJSONFile();
console.log(jsonData);
} catch (error) {
console.error('Error reading JSON file:', error);
}
}By using the toPromise method, the http.get request will be executed synchronously, allowing you to read the JSON file in a synchronous manner. However, keep in mind that this approach is not recommended for large JSON files or in scenarios where performance is critical.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 5, 2024, 12:15 AM EST
In Angular, reading data from a JSON file is typically done asynchronously using the HttpClient or Fetch API. However, you can use the
importstatement in TypeScript 2.9+ to read local JSON files in a way that behaves synchronously12. Here’s an example:import { Component, OnInit } from '@angular/core'; import * as data from './data.json'; @Component({ selector: 'app-root', template: ` {{product.name}} `, styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'Angular 15 JSON Example'; products: any = (data as any).default; constructor() {} ngOnInit() { console.log(data); } }In this example,
data.jsonis a local JSON file. Theimportstatement reads the file and assigns its content to thedatavariable. The data is then available for use in the component1.Please note that this method might not work if the JSON file is not local or if it’s not included in the build process. In such cases, you would need to use HttpClient or Fetch API, which are asynchronous123.
Also, keep in mind that JavaScript, including Angular, is single-threaded, and using synchronous operations, especially for I/O, can block the thread and lead to unresponsive interfaces. Therefore, it’s generally recommended to use asynchronous operations whenever possible.
If you need to ensure certain operations complete before others, consider using async/await or Promises to manage the order of operations. This allows you to write code that appears synchronous but actually executes asynchronously, thus not blocking the thread.
Remember to add and set the
resolveJsonModuleoption totruein thetsconfig.jsonfile under thecompilerOptionskey file to enable this featureThanks
Sachin SinghRank #66 · 26.2K reputationPosted Jan 4, 2024, 11:57 AM EST
// json-reader.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class JsonReaderService { constructor(private http: HttpClient) {} // Read JSON synchronously readJson(): Observable { return this.http.get('assets/data.json'); } } // json-reader.component.ts import { Component, OnInit } from '@angular/core'; import { JsonReaderService } from './json-reader.service'; @Component({ selector: 'app-json-reader', template: ` {{ jsonData | json }}