Extract JSON data from a web address using JavaScript

A unique system has been created to parse JSON and style it using CSS. Instead of outputting the data within the script, the goal is to retrieve data from a local or remote URL.

<script type='text/javascript'>
$(window).load(function(){
var jsonData={"users":[
        {
            "title":"Creative Design Tips",
            "content":"Insert text here. Keep it brief with 100 characters maximum per panel.",
            "url":"http://website.me/design_tips.php?=1234567890",
        },
        {
            "title":"UX/UI Trends",
            "content":"Jones",
            "url":"http://example.com",
        },
        {
            "title":"Digital Marketing Strategies",
            "content":"Smith",
            "url":"http://sample.com",
        },
        {
            "title":"Web Development Tools",
            "content":"Williams",
            "url":"http://test.com",
        }
]}
$(jsonData.users).each(function() {
    var result = "<a href='" + this.url + "'><div class='col-sm-12'><div class='panel panel-default'><div class='panel-body'><h4>" + this.title + "</h4><p>" + this.content + "</p></div></div></div></a>";
    $('#feed').append(result);
});
});
</script>

Is there a way to fetch JSON data from a URL source and display it in a similar fashion as shown above?

Update

<script type='text/javascript'>
    var newData;
    $.ajax({
    dataType: "json",
    url: `file://${__dirname}/new_data.json`,
    data: newData,
    success: function(newData) {
         newData = newData;
      }
    });
    $(window).load(function(){
    $(newData).each(function() {
        var result = "<a href='" + this.url + "'><div class='col-sm-12'><div class='panel panel-default'><div class='panel-body'><h4>" + this.title + "</h4><p>" + this.content + "</p></div></div></div></a>";
        $('#feed').append(result);
    });
    });
</script>

Answer №1

The recommended method is to utilize ajax, typically with the help of jQuery:

var jsonData;

$.ajax({
  dataType: "json",
  url: apiURL,
  data: requestData,
  success: function(response) {
       jsonData = response;
    }
});

For more information, you can refer to: https://api.jquery.com/jquery.ajax/

Answer №3

To retrieve JSON data from an external source, utilize the jQuery.getJSON() function.

Refer to the documentation for more information: http://api.jquery.com/jquery.getjson/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble viewing the CSS menu on Internet Explorer 8? Could it be a potential JavaScript issue causing the problem?

Receiving complaints about the menu bar dysfunction in Internet Explorer 8 has left me bewildered. Despite working perfectly on all other browsers and earlier versions of IE, I cannot figure out what's wrong with the code. You can view the website at ...

Rewriting JSON Key with Python to Achieve Effective Transformation

I have the following unique JSON: { "1593020400000": [ { "id": "42", "value": "42. Answer 1", "checked": true }, { & ...

Building a multilingual website using AngularJS UI-Router

I am currently working on developing a multilingual website using AngularJS. While providing translations in templates seems straightforward, I am facing challenges when it comes to implementing proper multilingual routes using UI-Router. Unfortunately, I ...

Having trouble getting Calendly Webhooks to function in a node.js environment with ngrok?

Hello everyone, this is my first time seeking help on Stack Overflow so please bear with me if there are any flaws in my question. I recently started using the Calendly Teams version and decided to implement the Webhooks feature on a Node.js web applicati ...

Error: Unexpected symbol "<" found in JSON at position 4

A JSON syntax error occurred at position 4, causing an Unexpected token < error message. The issue seems to be with the JSON.parse function in the jQuery library. Despite searching online and trying various solutions, I have been unable to resolve this ...

json-server does not rely on the code found within the middleware file

While working on front-end development, I decided to use json-server to mimic some API calls. However, I encountered an issue with adding a middleware: { "name": "my-app", "version": "0.1.0", "private": true, "scripts": { "serve": "concurrentl ...

The Jquery append function is limited to the initial ajax request, necessitating a page refresh in order to populate another div with the desired

When making an AJAX request to retrieve a JSON array, upon successful completion another AJAX request is triggered. The retrieved data is then populated into the div of a bootstrap modal using the jQuery append function. Everything functions as expected ...

Unable to Modify TextField Component in React

Is it possible to attach variables to TextField and have those variables deleted as a whole and not editable? Check out the Codesandbox example HERE code <CardContent> <Autocomplete value={values.variable} options={variables} ...

What is the method for assigning a string to module variable definitions?

As someone new to TypeScript and MVC, I find myself unsure if I am even asking the right questions. I have multiple TypeScript files with identical functionality that are used across various search screens. My goal is to consolidate these into a single fil ...

Generating a JavaScript object from a string to optimize its compatibility with datatables

This inquiry pertains to the plugin available at: var hidecols = '{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}'; var hidecolsobj = eval('(' + hidecols + ')'); ...

What is the process for creating a folder using Firebase Cloud Functions with Storage?

How do I create a folder named "posts"? Storage bucket path --> gs://app.appspot.com/posts Code to generate thumbnail from storage object exports.generateThumbnail = functions.storage.object() .onChange(event => { const object = event.data ...

"Enhance Your Website with jQuery: Organized Lists with Fixed Length, Connected,

I currently have a list that is separated into three columns, structured as follows: Column1 Column2 Column3 1 4 7 2 5 8 3 6 9 The list ranges from 1 to 9 and each column consists of a fixed number of rows (3). I am lo ...

Can the name of the Grunt task target be utilized within attributes?

I have implemented the grunt-replace task to make some content changes in the index.html file. However, I am looking for a way to avoid repeating code unnecessarily. The code snippet below is just an example of what I am trying to accomplish: replace: { ...

Using Vue.js to increment a value in an array every time a new row is added

In Vue.js, I am attempting to increment an array value when adding a new row. However, I encounter the following error message: You may have an infinite update loop in a component render function. The JavaScript logic looks like this: new Vue({ el: ...

Dealing with an Undefined Property: Troubleshooting Guide

Encountering an error message Uncaught TypeError: Cannot read property 'top' of undefined at VM32939 own.js:819 resulting from var hands_corrected = (hands_original.top + 680) this issue arises because "hands_original" is not used in any par ...

Tips for customizing the background color and image of a toaster

Looking to modify the background color and image based on the else condition (toaster.error) success: function (data) { if (data.message != ""){ toastr.success(data.message); ...

Learn how to retrieve values from a .json file in real-time and then perform comparisons with user input using Python

I have a JSON file structured like this: [ { "name": { "common": "Aruba", "official": "Aruba", "native": { "nld": { "official ...

Understanding how to extract data from a nested JSON response in ASP.NET MVC

I retrieved data successfully from a simple json using this link and displayed it on the view with the code below. SpeciesController.cs using diversity.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

The Problem of Restoring Column Height in Tabulator 4.6.3 Filters

The Issue After activating and deactivating header filters, the column height does not return to its original state. Is this the expected behavior? Is there a way to reset the column height? Check out this JS Fiddle example: https://jsfiddle.net/birukt ...

What is the most effective method for establishing a notification system?

My PHP-based CMS includes internal messaging functionality. While currently I can receive notifications for new messages upon page refresh, I am looking to implement real-time notifications similar to those on Facebook. What would be the most efficient ap ...