Reading a local JSON file in JavaScript using Fetch API

  Рет қаралды 106,101

one minute coder

one minute coder

Күн бұрын

#javascript #fetchapi
How to use JavaScript Fetch API to read a local JSON file and how show the result in console.
How to show fetched data in web developer console.
JavaScript Shorts:
• JavaScript Shorts

Пікірлер: 67
@ashwinajmery6071
@ashwinajmery6071 Жыл бұрын
Its feel rainy season...thats good
@5095smf
@5095smf Жыл бұрын
I love the rain sounds. Thanks for the tutorial!
@oneminutecoder
@oneminutecoder Жыл бұрын
Thanks
@exoaxse2256
@exoaxse2256 Жыл бұрын
thank you very much finally after a few day resolve thanks bro
@oneminutecoder
@oneminutecoder Жыл бұрын
Thanks
@Lighter7900music
@Lighter7900music Жыл бұрын
Thanks man it was helpful!
@danielsanchezmanez5153
@danielsanchezmanez5153 5 ай бұрын
It works. Amazing. Thank you
@NTFG
@NTFG 2 жыл бұрын
Put the sound of typing in your next video
@NTFG
@NTFG 2 жыл бұрын
@@itsmaxim01 cuz
@poknari
@poknari Жыл бұрын
my country here: Indonesia
@dilemma086
@dilemma086 6 ай бұрын
Thanks!!!
@laneW612
@laneW612 Жыл бұрын
thanks buddy!
@oneminutecoder
@oneminutecoder Жыл бұрын
Glad to help!
@ertekinertunc
@ertekinertunc Жыл бұрын
thanks man, quick and easy solution. I have a question, how can I move the variable inside the function out?
@oneminutecoder
@oneminutecoder Жыл бұрын
Şöyle kullanabiliriz let jsonData; fetch("test.json") .then(response => response.json()) .then(data => { jsonData = data; showInfo(); }); function showInfo() { console.log(jsonData); }
@oneminutecoder
@oneminutecoder Жыл бұрын
Veya böyle fetch("test.json") .then(response => response.json()) .then(data => { showInfo(data); }); function showInfo(data) { console.log(data); }
@oneminutecoder
@oneminutecoder Жыл бұрын
Ya da böyle async function fetchData() { const response = await fetch("test.json"); const data = await response.json(); console.log(data); } fetchData();
@ertekinertunc
@ertekinertunc Жыл бұрын
Çok teşekkür ederim 🙂🙂
@vignesh-l3i
@vignesh-l3i 2 ай бұрын
then how to print that data inside the document ?? bro plz replay .
@oneminutecoder
@oneminutecoder Ай бұрын
How to use response: kzbin.info/www/bejne/bJnTmJ56isulh6s kzbin.info/www/bejne/q6K0ooykrJaUhcUsi=9Rqd9-0ujbueya2r kzbin.info/www/bejne/q6K0ooykrJaUhcUsi=LM8WEjaVLUmEBZzN kzbin.info/www/bejne/qYvKn6mioLN9o6csi=CPcISYEl0kt--D-Q kzbin.info/www/bejne/q3WrqJmmqM6frassi=Iagk81OA3GO7Vg_S
@tirthpatel4624
@tirthpatel4624 Жыл бұрын
Similarly, is it possible to update/write data into json file using fetch or any other function..for me Im trying to update using fs but giving error as writeFile not function
@jimmlmao
@jimmlmao 11 ай бұрын
No, unless you write server side code to do that but thats a major security problem
@satishsgm0157
@satishsgm0157 Жыл бұрын
Do video on how to fetch json file from remote api also brother 😊
@oneminutecoder
@oneminutecoder Жыл бұрын
Isn't that already?
@antcannon
@antcannon 8 ай бұрын
new to APIs i am looking to find a way for this to show in my terminal. get an ERR_INVALID_URL error
@unknownhacker9416
@unknownhacker9416 7 ай бұрын
Can you please help me I have name and regarding mail I'd in json file when I dynamically call the name and mails to usin Javascript in my html code in that time i am getting only names not emails what is the cause
@gouravnainwaya5669
@gouravnainwaya5669 2 жыл бұрын
Does this are possible in axios in react native
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Yes possible. React Native has a built-in Fetch API. But i am not a developer of React Native.
@carloseduardomenendezm.2389
@carloseduardomenendezm.2389 8 ай бұрын
Hey i would like to know how did you do to read the local file because i always get an error message telling me something about cross references policy!!!!
@parveenchoudhary8258
@parveenchoudhary8258 2 жыл бұрын
Why it is working on live server but not on chrome.
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Fetch API makes HTTP requests to servers. Request URL should be URL: http... and not should be URL: file:///C:/Users/ if you use liveserver JavaScript sees test.json like this Request URL: 127.0.0.1:5500/test.json
@f4jrie09
@f4jrie09 Жыл бұрын
​@@oneminutecoder
@aryansoni8141
@aryansoni8141 Жыл бұрын
Which theme is this?
@oneminutecoder
@oneminutecoder Жыл бұрын
i don't remember.
@roselynbasan1326
@roselynbasan1326 11 ай бұрын
what compiler did you use?
@BizzaroBrainBoi
@BizzaroBrainBoi Жыл бұрын
dangggg how did you do that!!
@callowindia
@callowindia 2 жыл бұрын
Can we need localhost for this
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Yes. It doesn't work when I directly click and open the html file
@oneminutecoder
@oneminutecoder 2 жыл бұрын
In this example, i am using VS Code and Live Server Playlist for vs code kzbin.info/aero/PLrcsnb6QBvieVvHdr0X0M4uUiTSoGyAra
@bashirajasu5186
@bashirajasu5186 7 ай бұрын
But I want only one name and capital How to do that
@mr.RAND5584
@mr.RAND5584 8 ай бұрын
🇵🇭. Nice.
@TdukeTduke
@TdukeTduke Жыл бұрын
I cant get it to work either. What am I missing.
@oneminutecoder
@oneminutecoder Жыл бұрын
Run HTML file from within VS Code. Firstly add an HTML file Then paste this code inside it fetch("test.json") .then(response => response.json()) .then(data => showInfo(data)); function showInfo(data) { console.table(data.countries); } Add a JSON file test.json Then paste this code inside it { "countries": [ { "name": "Indonesia", "capital": "Jakarta" }, { "name": "Philippines", "capital": "Manila" } ] }
@oneminutecoder
@oneminutecoder Жыл бұрын
You should see data in console. Ctrl + Shift + J
@TdukeTduke
@TdukeTduke Жыл бұрын
@@oneminutecoder I got it to work now.. Thank you sir!!! I appreciate your response and commitment to help others learn!!
@mrstanley3037
@mrstanley3037 11 ай бұрын
Can I do this with txt file?
@oneminutecoder
@oneminutecoder 11 ай бұрын
YES. kzbin.info9Edc1day6p8
@biswajitmohanty54
@biswajitmohanty54 2 жыл бұрын
I have done the same way but fetch is not defined, could you please help
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Do you use VS Code?
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Maybe you are not using an HTML page and you are using Node.js
@biswajitmohanty54
@biswajitmohanty54 2 жыл бұрын
@@oneminutecoder yes I am using vs code
@biswajitmohanty54
@biswajitmohanty54 2 жыл бұрын
@@oneminutecoder I want to run the js file how you show on your video
@oneminutecoder
@oneminutecoder 2 жыл бұрын
Firstly add an HTML file
@umangternate
@umangternate Жыл бұрын
Anyone knows how to store data to a variable outside the fetch function?
@oneminutecoder
@oneminutecoder Жыл бұрын
Do you mean this ? let countries; fetch("restcountries.com/v3.1/all") .then(response => response.json()) .then(data => { countries = data; }) .then(() => { console.log(countries); });
@umangternate
@umangternate Жыл бұрын
@@oneminutecoder exactly, but if I call console.log outside the fetch function, it is empty. Scope issue, I guess.
@oneminutecoder
@oneminutecoder Жыл бұрын
@@umangternate Normally, for a code to run in JavaScript, previous codes must finish their work. But the fetch API doesn't work like that. Before the fetch finishes, the next code starts executing. So console.log(countries) runs before fetch brings countries. The codes waiting for each other are called synchronous, and the other is called asynchronous. That's why we use "then" in asynchronous structure. When you're done, do this. Why does fetch work asynchronously? For example, you will need to get weather forecasts from 10 different countries and you will connect to 100 different sources and get data. If you work synchronously, it will take too long. They should start fetching data at the same time without waiting for each other. This is what makes asynchronous structure necessary. fetch works like that. kzbin.info/www/bejne/rISvpmeIq7WBqJo kzbin.info/www/bejne/d6KyqGiXhbysa6M
@umangternate
@umangternate Жыл бұрын
@@oneminutecoder thank you for details...
@kirandev
@kirandev 9 ай бұрын
put some moaning sound in next video please 🙏
Add Event Listener in JavaScript
1:00
one minute coder
Рет қаралды 83 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,2 МЛН
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 72 МЛН
JavaScript Tip: Loading a Local JSON File without Fetch
9:05
All Things JavaScript, LLC
Рет қаралды 41 М.
JavaScript Fetch API - One Mistake I ALWAYS MAKE!
4:47
James Q Quick
Рет қаралды 30 М.
How To Import JSON in Excel
5:21
Tech Sessions
Рет қаралды 4,8 М.
1.4: JSON - Working with Data and APIs in JavaScript
16:22
The Coding Train
Рет қаралды 645 М.
The TSConfig Cheat Sheet
5:36
Matt Pocock
Рет қаралды 36 М.
Storing Objects with Local Storage in JavaScript
6:58
dcode
Рет қаралды 169 М.
Use Data Transfer Objects (DTOs) in .NET the Right Way 🚀
6:48
POST Form Data as JSON with Fetch API in JavaScript
9:25
ByteGrad
Рет қаралды 63 М.
How to FETCH data from an API using JavaScript ↩️
14:17
Bro Code
Рет қаралды 135 М.
Cómo pensaba que sería el iPhone 16 😭
0:12
Alan
Рет қаралды 2 МЛН
iPhone Standby mode dock, designed with @overwerk
0:27
Scott Yu-Jan
Рет қаралды 6 МЛН
Игровой руль - штука годная 👍
0:50
iPad Domino has been in use for a long time. Let's see the effect
0:12
3D Fantasy Life
Рет қаралды 517 М.