How to Read Local JSON file in Angular | JSON World (2024)

Today, I am going to create a sample application to show how to use local JSON file in Angular application. There area various way we can use local JSON file in our Angular application.

Let' Get Started

Step 1: Create an Angular application with Angular CLI

From terminal create a new angular app

ng newread-local-json-angular

Step 2: Create JSON file with dummy data

Create any JSON file with any name, I have created a file countries.json under _files inside app folder.

[

{"name" : "Afghanistan", "code" : "AF"},

{"name" : "Åland Islands", "code" : "AX"},

{"name" : "Albania", "code" : "AL"},

{"name" : "Algeria", "code" : "DZ"},

{"name" : "American Samoa", "code" : "AS"},

{"name" : "AndorrA", "code" : "AD"},

{"name" : "Angola", "code" : "AO"},

{"name" : "Anguilla", "code" : "AI"},

{"name" : "Antarctica", "code" : "AQ"},

{"name" : "Antigua and Barbuda", "code" : "AG"},

{"name" : "Argentina", "code" : "AR"},

{"name" : "Armenia", "code" : "AM"},

{"name" : "Aruba", "code" : "AW"},

{"name" : "Australia", "code" : "AU"},

{"name" : "Austria", "code" : "AT"},

{"name" : "Azerbaijan", "code" : "AZ"},

{"name" : "Bahamas", "code" : "BS"},

{"name" : "Bahrain", "code" : "BH"}

]

In the above, we file there is static list of countries with its name and code. Now in this app, I will show the data in app component.

Step 3: Import JSON file in the component

Update app.component.ts file, Have a look on the update app.component.ts file.

import { Component } from '@angular/core';

import countries from './_files/countries.json';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

title = 'json-file-read-angular';

public countryList:{name:string, code:string}[] = countries;

}

By default, Angular doesn't read the JSON file in the application. So we need to do some extra stuff for that. So we will create a file named 'json-typings.d.ts' inside the app folder of the project. Add below code in it.

declare module "*.json" {

const value: any;

export default value;

}

Step 4: Update Component Template file

Add below code inside app.component.html file

<div style="text-align:center">

<h2>

Country list from local json file...

</h2>

<ul *ngFor="let item of countryList">

<li>

<h3>Country Name :{{item.name}}, Code: {{item.code}}</h3>

</li>

</ul>

</div>

Now all the needed change has been done.

Step 5: Run the app

Run the app with npm start over terminal. App will look like below over browser.

How to Read Local JSON file in Angular | JSON World (1)

Conclusion

Using local JSON files in Angular applications is very easy, And it's very useful when we need to show some static data at frontend.

If you are new to Angular 8 thenfindAngular Sample Projectsto start the app for enterprise-level application

Let me know your thoughts over the emaildemo.jsonworld@gmail.com.I would love to hear themand If you like thisarticle, share with your friends.

Thank You!

How to Read Local JSON file in Angular | JSON World (2024)

FAQs

How to read a local JSON file in Angular? ›

Display Data from Json File in Angular
  1. Step 1: Create New App.
  2. Step 2: Create JSON File.
  3. src/app/students.json.
  4. Step 3: Update Ts file.
  5. src/app/app.component.ts.
  6. Step 4: Template code.
  7. src/app/app.component.html.

How do I read a local JSON file? ›

Your answer
  1. Mention the path of the json file in the script source along with the javascript file. <script type="text/javascript" src="data.json"></script> <script type="text/javascript" src="javascrip.js"></script>
  2. Get the Object from the json file. var mydata = JSON. parse(data); alert(mydata[0].
Sep 3, 2020

What is the easiest way to read a JSON file? ›

How To Open A JSON File On Windows, Mac, Linux & Android
  1. #1) File Viewer Plus.
  2. #2) Altova XMLSpy.
  3. #3) Microsoft Notepad.
  4. #4) Microsoft WordPad.
  5. #5) Notepad++
  6. #6) Mozilla Firefox.
Dec 5, 2022

How do I get items from local storage JSON? ›

Storing and retrieving objects with localStorage [HTML5]
  1. localStorage.setItem('user', JSON.stringify(user)); Then to retrieve it from the store and convert to an object again:
  2. var user = JSON.parse(localStorage.getItem('user')); If we need to delete all entries of the store we can simply do:
  3. localStorage.clear();
Jul 1, 2022

How do I get JSON data from local storage? ›

Summary
  1. Store data: localStorage.setItem(key, value)
  2. Read data: localStorage.getItem(key)
  3. To store JSON data, convert the value with JSON.stringify(value)
  4. To convert JSON data JSON.parse(value)
  5. In React, use useEffect to deal with side effects. ...
  6. In Angular, just use plain JavaScript inside ngOnInit, or where you prefer!
Oct 25, 2022

How do I make JSON text readable? ›

How do I convert a JSON file to readable?
  1. Open JSON to String tool and Copy and Paste JSON Code in Input Text Editor.
  2. If you do have a JSON data file, you can upload the file using the Upload file button. ...
  3. Click on JSON to String button once data is available in Input Text Editor, via Paste, File, or URL.

How to read JSON file from assets folder in Angular 8? ›

One way to read a JSON file from the assets folder in Angular is to use the import statement in your component. If you are learning Angular and didn't meet HttpClient yet, this is the easiest way.

How do I decode a JSON file? ›

Decoding JSON data is as simple as encoding it. You can use the PHP json_decode() function to convert the JSON encoded string into appropriate PHP data type.

How to view JSON in readable format? ›

To open a JSON file, you can: Use Notepad++ Use Microsoft WordPad. Use a7 JsonViewer.

Which method is used to read data from JSON? ›

You can use json. load() method to read a file containing JSON object.

What is the best JSON file viewer? ›

Best jQuery JSON Viewer Tools
  • Render JSON Data As A Tree View – json-view.
  • Simple Folder Tree With JSON And JavaScript – tree. ...
  • Minimal JSON Data Formatter – JSONViewer.
  • Beautiful JSON Rendering In Pure JavaScript – JSON Formatter.
  • JSON Viewer & Path Picker In Vanilla JavaScript.
Mar 13, 2022

How do I get data from local storage in Angular 10? ›

To use it, first install it in your Angular project using npm . Once you have it installed, you can import the library in the LocalService . Now, you can use the above two methods to encrypt and decrypt before storing and fetching data from local storage. Save the Angular app and run the application.

How to read local JSON file in Angular 12? ›

Angular 12 – How to Load JSON Data from Assets Folder?
  1. How to Display JSON data in Table in Angular App?
  2. Step 1 – Create and Setup Angular Project.
  3. Step 2 – Create JSON File in Assets Folder.
  4. Step 3 – Install & Config Bootstrap.
  5. Step 4 – Import Employees JSON & Create Interface.
  6. Step 5 – Create Table with Bootstrap Style.
Nov 13, 2022

How do I get data from local storage in Angular 8? ›

Local Storage Methods
  1. setItem(key: string, data: string | JSON): void. Takes a key parameter and a value parameter. ...
  2. getItem(key: string): string | JSON | null. Takes a key parameter for looking up data in storage. ...
  3. removeItem(key: string): undefined. Takes a key parameter for looking up data in storage. ...
  4. clear(): undefined.
Jan 24, 2021

How to get the value from local storage in Angular? ›

You can get/set the localStorage value by using the getItem/setItem method in window. localStorage. //get the Grid model. let value: string = window.

How do I view local storage data? ›

You can read data from localStorage using the getItem() method. This method accepts the key as a parameter and returns the value as a string. If you want to convert the result from a string to an object, you should use the JSON. parse() method.

How do I get specific data from local storage? ›

To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.

What is local storage in JSON? ›

With localStorage , web applications can store an entire user-authored document on the client-side for performance reasons, but cookies do not handle this well, as they're transmitted with every request made to the server. Storage capacity for cookies is only about 4KBm in contrast to localStorage capacity of 10MB.

Can you save JSON in local storage? ›

JSON stands for “JavaScript Object Notation” and is a method for representing complex data types as strings. It's useful for localStorage, and it's also used for sending information back and forth over the web. It's a subset of JavaScript's object syntax.

How to get local storage array value? ›

Get Array From localStorage

If you want to retrieve the array from the local storage, you can use the getItem method. The method takes only one parameter, the "key", and returns the "value" in a string format. The last line of code retrieves the array from the local storage and converts it from a string to an array.

What is the best text editor for JSON? ›

Best & Most Popular 5 Code Editor for Json programming Language
  • Visual Studio Code.
  • Sublime Text 3.
  • Atom.
  • GEANY.
  • Spacemacs.
Apr 1, 2022

How big is too big for a JSON response? ›

How large can JSON Documents be? One of the more frequently asked questions about the native JSON data type, is what size can a JSON document be. The short answer is that the maximum size is 1GB.

How do I read and edit a JSON file? ›

Procedure. In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.

How do I view a JSON file in maps? ›

Display Json data on Google Maps
  1. Step 1: Please navigate to the “WP MAPS PRO -> JSON File To Map”.
  2. Step 2: Please select your map from the given 'Select Map' dropdown to add locations on a map.
  3. Step 3: After selecting a map you will see one more dropdown “Choose JSON File” is visible here, please upload a JSON file.

How can we access data inside a JSON object? ›

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How do you read an object from a JSON object? ›

Example - Parsing JSON

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How does JSON decoder work? ›

The JSONDecoder class decodes instances of a data type from JSON objects. The data type must conform to the Decodable protocol; it can be either predefined types like String , Double , or Date or custom classes, enumerations, or structures.

How to fetch data from JSON file in JavaScript? ›

We can read this JSON data in JavaScript using the import statement this way: <! ---./index. js--> import data from './data.

What is the use of JSON decode? ›

The json_decode() function can take a JSON encoded string and convert into a PHP variable. The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively.

How to read JSON data in Angular 8? ›

Import & Read Local JSON Files in Angular 7/8 and TypeScript 2.9+
  1. Step 1 - Preparing your Angular 8 Project. You should create an Angular 8 project using Angular CLI.
  2. Step 2 - Configuring the TypeScript Compiler. ...
  3. Step 3 - Creating a Local JSON File. ...
  4. Step 4 - Importing the JSON File. ...
  5. Step 5 - Displaying the JSON Data.
Oct 20, 2019

How to import a JSON file in Angular? ›

First Way
  1. Step 1 : Create your JSON file in your project wherever you want. ...
  2. Step 2 : Set the resolveJsonModule: true & esModuleInterop: true in the tsconfig. ...
  3. Step 3 : Now you can import JSON file in your components/directives/services wherever you want with // PATH TO YOUR JSON FILE.

How to use JSON file in Angular 12? ›

  1. Step 1 – Create and Setup Angular Project. ...
  2. Step 2 – Create JSON File in Assets Folder. ...
  3. Step 3 – Install & Config Bootstrap. ...
  4. Step 4 – Import Employees JSON & Create Interface. ...
  5. Step 5 – Create Table with Bootstrap Style. ...
  6. Step 6 – Update Compiler Options in TS Config. ...
  7. Step 7 – Run Angular App.
Nov 13, 2022

Where is Angular JSON file located? ›

The angular. json file is always found in the root folder of a CLI generated project, along with the package. json file.

How to run db JSON in Angular? ›

4 - Add JSON server
  1. npm install json-server and ts-node.
  2. Make a server. ts file in a new folder called server.
  3. Add a file called db.json.
  4. Add the below script to the scripts in the package.json.
  5. Start the server and leave it running.
  6. Navigate to http://localhost:3000​
  7. Commit your code.

How to read JSON file from assets in Angular? ›

Read a JSON File From Assets in Angular
  1. Using the import statement. ...
  2. Using Angular HttpClient. ...
  3. Other ways to read a JSON file from assets in Angular. ...
  4. 3.1 Add a typing file — json-typings. ...
  5. 3.2 Use the fetch API. ...
  6. 3.3 Use Angular JsonPipe.
Oct 17, 2022

What is the use of JSON parse in Angular? ›

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

What is toJSON () in JSON? ›

toJSON() calls the object's toISOString() method, which returns a string representing the Date object's value. This method is generally intended to, by default, usefully serialize Date objects during JSON serialization, which can then be deserialized using the Date() constructor or Date. parse() as the reviver of JSON.

How do I run JSON on a local server? ›

How to use JSON Server to mock REST API in local environment
  1. Install JSON Server. Open your Terminal. ...
  2. Go to the folder where you want to store the json files. ...
  3. Start JSON Server with the specified json file. ...
  4. Verify the mocked API. ...
  5. If you want to have additional fake API, repeat above steps but specify different ports.
Jul 12, 2019

What is the syntax to read JSON format file? ›

We can read this JSON data in JavaScript using the import statement this way: <! ---./index. js--> import data from './data.

What happened to Angular CLI JSON? ›

angular-cli. json file has been replaced by angular. json since Angular CLI v6-RC2. An Angular Workspace is a project which is produced by the ng new command.

What is the difference between Angular JSON and package JSON? ›

Package. JSON holds all of the “npm” packages installed for the project. Angular. JSON holds the configuration for the project.

How to read properties file in Angular 8? ›

Hit F12 (or Cmd-Alt-I) on Mac. Go to the network panel. Look at the HTTP request for this "properties" file (which is in fact a JSON file). Check that the response is what you expect it to be.

How do I view a JSON file in TypeScript? ›

The resolveJSONModule option allows us to import modules with . json extension in our TypeScript files.
...
To import a JSON file in TypeScript:
  1. Set resolveJsonModule to true in your tsconfig. json file.
  2. Set esModuleInterop to true in tsconfig. json .
  3. Import the JSON file as import employee from './employee. json' .

Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 5635

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.