How to read JSON files into HTML using JavaScript's fetch (no D3, no jQuery, vanilla JS!)

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 พ.ย. 2024

ความคิดเห็น • 79

  • @binauralfortune
    @binauralfortune 2 ปีที่แล้ว +6

    thanks for this, Jonathan. The fetch function was the straight-forward approach I needed for my simple project. No need to use npm, install frameworks and bloated libraries. I got the js script to grab the text inside my json file and update my html. Success! I got my dopamine hit for the day and now I can pull away from the computer, get some fresh air, walk down to the local Starbucks and celebrate with a "java" chip frappuccino.

  • @gabrielvindas4373
    @gabrielvindas4373 3 ปีที่แล้ว +28

    I have literally spent like 3 hours trying to do this. Thank you!!

    • @beep3639
      @beep3639 3 ปีที่แล้ว

      Same

  • @rafaelibanhez5501
    @rafaelibanhez5501 3 ปีที่แล้ว +2

    Man.... I been looking for this for a long time. You made my day. Thank you.

    • @thelonercoder5816
      @thelonercoder5816 2 ปีที่แล้ว

      same bruh same lmao.. literally been searching for HOURS!

  • @alireza.tayari
    @alireza.tayari 3 ปีที่แล้ว

    Loved the way you simplify thinks Jonathan

  • @Btechdom
    @Btechdom 3 ปีที่แล้ว +9

    If you're getting [object Object], just wrap your JSON variable like:
    .innerText = JSON.stringify(data);

  • @ae6905
    @ae6905 3 ปีที่แล้ว

    Was suffering without this video. Thank you so much.

    • @jsoma
      @jsoma  3 ปีที่แล้ว

      Glad it was helpful!

  • @digitalart9272
    @digitalart9272 3 ปีที่แล้ว

    bruuuhh..! finally i get the right video, i spent more than 4 hours searching thank youu

  • @i_clock4783
    @i_clock4783 ปีที่แล้ว

    thanks for this video i was looking for something like this for ages

  • @zahabiyafakhruddinrangwala1564
    @zahabiyafakhruddinrangwala1564 2 ปีที่แล้ว

    I cannot thank you enough for your video! you are a great savior!

  • @chunkbot
    @chunkbot 3 ปีที่แล้ว

    This was super helpful today, for me, and helped me overcome a hurdle in my understanding of how javascript works.

  • @techz-777
    @techz-777 ปีที่แล้ว

    Thank you so much, i watched like 10 videos and got the same error. your vid helped me alot

  • @frango_e_salada
    @frango_e_salada 2 ปีที่แล้ว

    JEzz! 3 days searching!! Tks-a-LOT!!!

  • @shubhamshrivastav9572
    @shubhamshrivastav9572 2 ปีที่แล้ว

    now this is what I was looking for...thanx a lot man...u just earned a subscriber 👍👍👍

  • @saas2887
    @saas2887 3 ปีที่แล้ว

    I was trying to find something like that whole day and i came across you.Thank you so much mate.

  • @seoro4380
    @seoro4380 2 ปีที่แล้ว

    Thank you 😂 I was looking for how to use fetch.

  • @richhutnik2477
    @richhutnik2477 2 ปีที่แล้ว +1

    I am having problems here trying to do this for the Cloud Resume Challenge. I want to extract values from a JSON file and manipulate them. I want to at least convert the JSON file into a single string I can parse into another piece of code and I am stuck. I get the JSON object to display in the console, but I cannot get it to work elsewhere. I know about the CORS issue, which I did resolve, but I am still stuck.

  • @ssmishra78
    @ssmishra78 3 ปีที่แล้ว +1

    Fetch method is not working for me. I am using node js server. Any solution please

  • @kolyxix
    @kolyxix 3 ปีที่แล้ว

    Thank you so much. I have spent the whole day looking a solution and I found this. God bless you

  • @ht8702
    @ht8702 3 ปีที่แล้ว

    Nice vid Jonathan, good clear explanation.

  • @fijyfree
    @fijyfree 2 ปีที่แล้ว

    Very good video, nice funny speech :)
    Thanks for help buddy!

  • @zahidshaikh3122
    @zahidshaikh3122 3 ปีที่แล้ว +3

    Amazing Explanation, Thanks a lot.

  • @user-jb2vv1ie2w
    @user-jb2vv1ie2w 3 ปีที่แล้ว +1

    is that working on Live server extension that vscode provides?
    (Edited)
    Yes

  • @lorenzosandini1131
    @lorenzosandini1131 3 ปีที่แล้ว +3

    Very nice, thank you.
    What about arrays ? If the content of my sample-file.json is the following, and I need to retrieve the "notes" and "created_at" key values ?
    [
    {
    "enteredBy": "Boss",
    "eventType": "Announcement",
    "notes": "glargine 14",
    "created_at": "2021-03-23T19:00:25.889Z",
    },
    {
    "created_at": "2021-03-23T17:27:29.472Z",
    "eventType": "Meal Bolus",
    "carbs": 40,
    "insulin": null
    }
    ]

  • @Donnshin
    @Donnshin 2 ปีที่แล้ว

    You are a life saver, cheers.

  • @shailjasharma8398
    @shailjasharma8398 3 ปีที่แล้ว +1

    how do we catch the data so that we can use it later in the code, ie. outside of fetch, I tried something like .then(Data => json_obj = Data)
    but as soon as we get out of the fetch function it no longer recognises json_obj

  • @technicalnitians8968
    @technicalnitians8968 3 ปีที่แล้ว

    How to update this json file using vanilla JS

  • @suranjandaw241
    @suranjandaw241 3 ปีที่แล้ว

    Thanks man,. Very Helpful video.

  • @santoshsingh5864
    @santoshsingh5864 2 ปีที่แล้ว

    /// I Have Used this fetch but they will given error // please give some instruction any body
    fetch("card.json")
    .then(tha=>tha.json)
    .then(data3=>{
    console.log(data3);
    })

  • @lukebedrosian8397
    @lukebedrosian8397 3 ปีที่แล้ว

    I think I get it but how might I make my data into a list, also my json that turns into an html just says object?

  • @suzanmagar6806
    @suzanmagar6806 3 ปีที่แล้ว

    finally, it worked, thanks a lot

  • @sumanyuasthana1435
    @sumanyuasthana1435 3 ปีที่แล้ว

    I can access the data when I do all this but what when I publish the website?

  • @DrowninGGaming
    @DrowninGGaming 3 ปีที่แล้ว

    Brilliant explanation, thank you very much! Subscribed!

  • @lorenzlandero5013
    @lorenzlandero5013 2 ปีที่แล้ว

    hello it says error response on my webbrowser

  • @sanchogodinho8769
    @sanchogodinho8769 3 ปีที่แล้ว +1

    Thanks A Lot! It Really Helped :)

  • @underworld-of-gaming
    @underworld-of-gaming 2 ปีที่แล้ว +1

    Thankyou very very very much for this video ❤️❤️😘😘😘😘😘

  • @johiny
    @johiny 3 ปีที่แล้ว

    thx man very good tutorial and explanation!

  • @GucciJohanne
    @GucciJohanne 2 ปีที่แล้ว

    lifesaver king !!!!

  • @robelmebrahtu5453
    @robelmebrahtu5453 3 ปีที่แล้ว

    Hi, Do you have a video that shows how to create a website that reads a jason file directly from AWS cloudntrial logs or JSON file from local host? Thanks

  • @franckviera3860
    @franckviera3860 2 ปีที่แล้ว

    I have a question, when yo want to put this files to the web hosting, does it still works?

  • @Extravi
    @Extravi 2 ปีที่แล้ว

    Thank you so much!

  • @JohnBartmannMusic
    @JohnBartmannMusic 2 ปีที่แล้ว

    amazing, thanks. nice and simple.

  • @celebratethedays9409
    @celebratethedays9409 3 ปีที่แล้ว

    Thank you! Helped me solve my problem.

  • @killer0200318
    @killer0200318 3 ปีที่แล้ว +1

    Thanks for the great video. Would doing this in react be any different?

  • @bhumeeshah9613
    @bhumeeshah9613 3 ปีที่แล้ว

    adding this script is not letting my css file load

  • @aditijain9865
    @aditijain9865 3 ปีที่แล้ว +1

    This video is so helpful! Thank you! What if we have multiple arrays in the JSON file that we want to write into the HTML file one after another. When I tried copy-pasting the two lines for the other arrays, it just overrides the original one in the html file. Do you have any tips for how to solve this?
    console.log(data.sentence)
    document.querySelector("#debug").innerHTML = data.sentence

  • @haider_javaid
    @haider_javaid 3 ปีที่แล้ว +1

    5:54 Minecraft damage taken sound effect

  • @kuldeep7063
    @kuldeep7063 ปีที่แล้ว

    now how to write and modify json data..........

  • @rodrigobazanzurita4457
    @rodrigobazanzurita4457 3 ปีที่แล้ว

    and then What I do to see my json in this html?

    • @jsoma
      @jsoma  3 ปีที่แล้ว

      It depends on what you're trying to do! Check around 5:14, it might be what you're looking for to insert parts of the JSON data into your HTML.

  • @JP-vi9mw
    @JP-vi9mw 3 ปีที่แล้ว

    FINALLY!!!!!!!!! I LOVE YOU

  • @zhabiboss
    @zhabiboss 2 ปีที่แล้ว

    Bros vid quality is on life support 💀

  • @monkyout
    @monkyout 2 ปีที่แล้ว

    Thank you very much.

  • @rajbannasa7662
    @rajbannasa7662 3 ปีที่แล้ว

    Thank you so much sir ❤️

  • @djkeiran2172
    @djkeiran2172 2 ปีที่แล้ว

    can you send code

  • @salamelek9603
    @salamelek9603 3 ปีที่แล้ว

    Thank you for this

  • @shivaamchourasia
    @shivaamchourasia 3 ปีที่แล้ว

    can we render image using this?

  • @batumanav
    @batumanav 3 ปีที่แล้ว

    Hello, thanks for the tutorial. There is a JSON data that I want to extract from a webpage, I can visually see the data when I right click and View Page Source. However, the data is below and inside of a eventlistener.
    window.addEventListener("DOMContentLoaded", function() {
    renderClientSide(....json data...)})
    Like this. Can I use a similar method to extract this data? Or is there any other way?

  • @whoozy1555
    @whoozy1555 3 ปีที่แล้ว

    What if my file is on a syntology diskstation and not on my pc? I have the website on my diskstation and want to read the json file which is also on the diskstaion. Does anyone know a answer for that? Thank you

    • @jsoma
      @jsoma  3 ปีที่แล้ว +2

      I've never tried, but I have some ideas! I believe you can sync from a DiskStation to Amazon S3 (might depend on your model) - if you watch the others in the series you'll see how to connect your JavaScript to S3. If you only want it locally on your machine you could use Web Station to start a web server on your synology, and have the URL point to that. Or put your index.html/etc on the diskstation in the same folder as the JSON and use it as a shared drive.

  • @Hasan-ye3xz
    @Hasan-ye3xz 5 หลายเดือนก่อน

    cant thank enough !!

  • @harbaapkabaap2040
    @harbaapkabaap2040 3 ปีที่แล้ว

    You got it, you're fine!! 🤣😂🤣

  • @jimtron3256
    @jimtron3256 3 ปีที่แล้ว

    How can I do the same thing but with.txt files?

    • @jsoma
      @jsoma  3 ปีที่แล้ว +1

      Instead of response.json(), you use response.text()!

  • @ProfessorLuxi
    @ProfessorLuxi ปีที่แล้ว

    ez tut. thx

  • @crazyupdates2857
    @crazyupdates2857 4 ปีที่แล้ว

    can i know what is the command that i can to get rid of the URL scheme must be "http" or "https" for CORS request through Javascript instead of Python?

    • @ChannelSeSe
      @ChannelSeSe 3 ปีที่แล้ว

      You must install an addon called live server on your Visual Studio Code because fetch does not support file://
      I think you can use fetch and open your index.html in firefox but you have to configure it to support file://. Once you install live server you can run your index.html or another HTML page in chrome but you have to choose the option Open with Live Server in yours Visual Studio Code and it's work. I hope I have helped you.

  • @duanecosta
    @duanecosta 3 ปีที่แล้ว

    Great, thanks!!

  • @TakeAguess323
    @TakeAguess323 2 ปีที่แล้ว

    Thank you

  • @JudithOpdebeeck
    @JudithOpdebeeck 3 ปีที่แล้ว +1

    we're gonna use just plain ole javascript. except we'll also use python of course...

    • @jsoma
      @jsoma  3 ปีที่แล้ว

      You're free to just upload it all to the internet and it'll work without it! Or use Firefox from like 2009 when security was nbd and you could read files from your comp willy-nilly.

  • @gonzalolopez2284
    @gonzalolopez2284 3 ปีที่แล้ว

    I love you. Tanks

  • @devwayagence9718
    @devwayagence9718 3 ปีที่แล้ว

    ma man

  • @omorxfe
    @omorxfe 3 ปีที่แล้ว

    pog

  • @rickpo2248
    @rickpo2248 3 ปีที่แล้ว

    If it is not a sentence but array, what do I put?