JavaScript Tip: Loading a Local JSON File without Fetch

  Рет қаралды 43,125

All Things JavaScript, LLC

All Things JavaScript, LLC

Күн бұрын

Пікірлер: 72
@septimusforster
@septimusforster Жыл бұрын
God bless you, sir. You're one of the few finest KZbin instructors on JavaScript, including Kyle from WebDev Simplified and Professor Steve Griffith.
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Glad to help! Thanks!!
@NicoHeinrich
@NicoHeinrich 2 жыл бұрын
This is exactly what I was looking for ... makes my code so much more streamlined!
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Glad it helped!
@junk1ord
@junk1ord 2 жыл бұрын
I've been searching for hours and ended up on this video. Thanks a million tons!
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Glad I could help!
@darrenlo1165
@darrenlo1165 2 жыл бұрын
This only works with Chrome. It does not work with FF just a heads up. If you try to run this with FF you will have issues and it will not load the array since FF does not support import assertions.
@ahmedalsagir
@ahmedalsagir 2 жыл бұрын
i can't stop watch 3 hour continuous, amazing
@karlstenator
@karlstenator 2 жыл бұрын
Thanks for sharing this. I found this example to work well when using Google Chrome as the target, but when using FireFox - it reports; "Uncaught SyntaxError: import assertions are not currently supported".
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Thanks for reporting that firefox doesn't support insertions yet!
@ivangutowski-smith6514
@ivangutowski-smith6514 2 жыл бұрын
Thanking for saying you would link something in the description, and remembering to link it
@abduljamadar9925
@abduljamadar9925 4 ай бұрын
I was looking for this solution from past 2 days. Thank you sir.
@AllThingsJavaScript
@AllThingsJavaScript 4 ай бұрын
Glad it helped!
@mikhailwebb8377
@mikhailwebb8377 2 жыл бұрын
Everything else seems to work for me now. BUT, I need the .json file to read as an object or variable like dictionary so i can process the data from it in Javascript. How can i store the data that is read into a variable like object? I need it to be able to read similar to how python read dictionaries
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
When the JSON file is parsed, it is a JavaScript object that you can work with. Perhaps these tutorials will help: kzbin.info/www/bejne/hpOQaXewdrSVo8k and kzbin.info/www/bejne/r4CZaIpqqalphrM
@mikhailwebb8377
@mikhailwebb8377 2 жыл бұрын
@@AllThingsJavaScript Thanks for your reply. I resorted to using python to finish the implementation.
@4estral
@4estral Жыл бұрын
At this time, Firefox still doesn't support assertions. Otherwise this works great.
@umangternate
@umangternate Жыл бұрын
I got "import assertions are not currently supported"
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Are you running your files from a local server?
@umangternate
@umangternate Жыл бұрын
@@AllThingsJavaScript yes sir, I was using WAMP server. But I figured out that it was the browser. Mozilla FF does not support it currently. When I use Chrome, it ran smoothly.
@madhavig5302
@madhavig5302 11 ай бұрын
Simple and Stright to the point. Thank you.
@AllThingsJavaScript
@AllThingsJavaScript 11 ай бұрын
You're welcome!
@ArvindMMittal
@ArvindMMittal 6 ай бұрын
Great! So simple.
@cxkcxk-o6d
@cxkcxk-o6d Жыл бұрын
So how do you resolve "Cross origin requests are only supported for HTTP."
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
It requires among other things changes on the server. Search resolving cross origin requests for lots of info on it.
@abdullahkhurram9847
@abdullahkhurram9847 2 ай бұрын
still i have a problem : Uncaught SyntaxError: Unexpected identifier 'assert' (can you help???)
@AllThingsJavaScript
@AllThingsJavaScript 2 ай бұрын
Can you provide a bit more information? Where is assert in your code? Maybe provide you code.
@Erebor99700
@Erebor99700 Жыл бұрын
Thx, such useful info! Warmest greetings from Ukraine!
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Glad it was helpful! Our prayers our with everyone in Ukraine.
@_V__.
@_V__. 2 жыл бұрын
Many thanks for this, I’ve sometimes wanted to try some stuff out using a mock data file i JSON format, but I’ve usually given up after getting that second error message. Now I know why it happens.
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Glad it helped!
@oreki1996
@oreki1996 2 жыл бұрын
newbie here! Is it possible to update the content of the JSON file?
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
The best way to update a JSON string is to convert to an object (JSON.parse) update the object and then convert back to a string (JSON.stringify).
@oreki1996
@oreki1996 2 жыл бұрын
@@AllThingsJavaScript will try! thank you so much
@jayantagarwal6339
@jayantagarwal6339 2 жыл бұрын
you really saved my day
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Glad to help!
@nyarlathotepthecrawlingcha969
@nyarlathotepthecrawlingcha969 2 жыл бұрын
I have a question. How can I import data of a nested array in the JSON i have made a script to pull data from an SQL server, which works fine but outputs a file set like this [ [ { Objects } A bunch more objects ] ] And I've been using a script I've found to display it in HTML, but the script doesn't understand the nested array
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Once you have the JSON data, you just need to access it like a normal array/object. See if this tutorial helps: kzbin.info/www/bejne/r4CZaIpqqalphrM Or maybe this one kzbin.info/www/bejne/n2PImGWuecedg5I
@prinzadamz
@prinzadamz 3 ай бұрын
Thanks a ton 🤘👍
@AllThingsJavaScript
@AllThingsJavaScript 3 ай бұрын
Glad it helped!
@xc4945
@xc4945 Жыл бұрын
I got an error "Uncaught SyntaxError: Unexpected token ':' " for my json file
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Looks like there may be an error in your code or file. A colon where it shouldn't be. Post if you need some help finding it.
@TillmanTech
@TillmanTech 2 жыл бұрын
Thanks! Excellent video. This method is great for when your app’s json data is on the same server as the app itself. No need to wait for the json. Promises can be avoided making coding easier/less complex
@HollowsDarkness
@HollowsDarkness 2 жыл бұрын
Then could you console.log(users.name) and it print out the ane of the users?
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Once you load the JSON file you can access it like any JavaScript object. So you could console.log any data it contains.
@HollowsDarkness
@HollowsDarkness 2 жыл бұрын
@@AllThingsJavaScript gotcha then i must be doing something wrong lol, i can console.log the variable i attached the JSON to but if i try to do it grab the name or IDs from the variable it just comes back as undefined. I'll have to look more into it, thanks!
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
@@HollowsDarkness If you need to, post your code.
@HollowsDarkness
@HollowsDarkness 2 жыл бұрын
@@AllThingsJavaScript here is my code, import pokemon from './pokemon.json' assert {type: 'json'} let pokemonString = JSON.stringify(pokemon) let poke = [] // for (let p in pokemonString) { // poke.push(pokemonString) // } for (let i = 0; i < pokemonString.length; i++) { const p = pokemon[i]; poke.push(p) } console.log(poke)
@JIucmuk
@JIucmuk Жыл бұрын
thanks a lot!
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Welcome!
@АлександрШтыб-е5т
@АлександрШтыб-е5т Жыл бұрын
Thank you very much! You helped me out
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Glad I could help!
@frechdaXchs
@frechdaXchs 2 жыл бұрын
does not work for me. Need to access a local json file without a (live) server or user input. Seems impossible...
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
For security reasons that is not allowed. A browser is not given access to files on your hard drive. As you can imagine there would be a lot of ways to use that against you.
@darkknight7623
@darkknight7623 Жыл бұрын
thank you sir, it helps a lot
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Most welcome!
@roumen1669
@roumen1669 Жыл бұрын
Thank you so much!
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Glad it helped!
@angelgarcia6911
@angelgarcia6911 2 жыл бұрын
Thank you!
@EmelieBoss
@EmelieBoss Ай бұрын
great!
@perdu0609
@perdu0609 Жыл бұрын
Does anyone have an error "Cannot use import statement outside a module" ?
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Did you set up your JavaScript file to be a module in your HTML file?
@fayyu5506
@fayyu5506 6 ай бұрын
this is still giving cors error
@AllThingsJavaScript
@AllThingsJavaScript 6 ай бұрын
Do a google search on cors errors for localhost. There is a lot of information out there.
@MattiaRuberto
@MattiaRuberto Жыл бұрын
Mind-blowing!!!
@everestobot3934
@everestobot3934 Жыл бұрын
Local Filesystem Detected ! Put this to your header:
@avertry9529
@avertry9529 2 жыл бұрын
Thanks, but your link to fetch, actually links to this video.
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Thanks for letting me know.
@mutamente
@mutamente Жыл бұрын
pouts!! que montagem huahuahuahuauha muito bom xD
@Assiel1
@Assiel1 Жыл бұрын
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file///C:/XXX/app/js/app.json 🥲
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
What are you using for your local server? There may be documentation about disabling Same Origin Policy
JavaScript Tip: Locating an Object in an Array
9:40
All Things JavaScript, LLC
Рет қаралды 7 М.
1.1: fetch() - Working With Data & APIs in JavaScript
15:39
The Coding Train
Рет қаралды 457 М.
Andro, ELMAN, TONI, MONA - Зари (Official Music Video)
2:50
RAAVA MUSIC
Рет қаралды 2 МЛН
Ozoda - Alamlar (Official Video 2023)
6:22
Ozoda Official
Рет қаралды 10 МЛН
Their Boat Engine Fell Off
0:13
Newsflare
Рет қаралды 15 МЛН
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,3 МЛН
Урок 17. JavaScript. Все о LocalStorage
16:39
Владилен Минин
Рет қаралды 155 М.
Read/Write JSON Files with Node.js
10:27
Hey Node
Рет қаралды 90 М.
Делаем мультиязычный сайт c JavaScript
24:47
WebDev с нуля. Канал Алекса Лущенко
Рет қаралды 65 М.
How to FETCH data from an API using JavaScript ↩️
14:17
Bro Code
Рет қаралды 179 М.
JavaScript Tip: Accessing JSON Data
11:07
All Things JavaScript, LLC
Рет қаралды 54 М.
1.4: JSON - Working with Data and APIs in JavaScript
16:22
The Coding Train
Рет қаралды 648 М.
Python Tutorial: Working with JSON Data using the json Module
20:34
Corey Schafer
Рет қаралды 1,1 МЛН
8.5: Saving Data to JSON File with Node.js - Programming with Text
17:00
The Coding Train
Рет қаралды 225 М.
Import your JSON and CSS as ES Modules
8:40
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 7 М.
Andro, ELMAN, TONI, MONA - Зари (Official Music Video)
2:50
RAAVA MUSIC
Рет қаралды 2 МЛН