Linking JSON information to an HTML document

I'm struggling to integrate some JSON data into an HTML page using "Vanilla Javascript," but I'm unsure of how to proceed. Can you please provide assistance? I can share the HTML, CSS code, and an example of the JSON data I want to incorporate into the HTML page.

The JSON data resides in multiple locations within the HTML files, and I need to connect it with my HTML content, but I lack the knowledge on how to achieve this!

h1 {
  text-align: center;
}

ul {
  list-style: none;
}

.flexcontainer {
  display: flex;
  align-items: center;
}

.container {
  padding-top: 1%;
  padding-bottom: 1%;
  margin-top: 1%;
  margin-bottom: 1%;
}

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Employee's Page</title>
    <link rel="stylesheet" href="./assets/css/style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
        crossorigin="anonymous">
</head>

<body>
    <h1>Name of the chosen person</h1>
    <div class="container" style="border:1px solid #cecece;">

        <div class="flexcontainer">
            <div>
                <img src="http://placehold.it/350x250" alt="">
            </div>
            <div>
                <ul>
                    <li>
                        <h3>Full name: (Json Data)</h3>
                    </li>
                    <li>
                        <h3>Gender: (Json Data)</h3>
                    </li>
                    <li>
                        <h3>Phone number: (Json Data)</h3>
                    </li>
                    <li>
                        <h3>Company: (Json Data)</h3>
                    </li>
                    <li>
                        <h3>Address: (Json Data)</h3>
                    </li>
                </ul>
            </div>
        </div>

        <div>
            <h3>About employee:</h3>
            <p>(Json Data)</p>
        </div>

        <div>
            <h3>Employee was registered in the system: (Json Data)</h3>
        </div>
        
    </div>
</body>
<script src="./assets/js/json.js"></script>


</html>

The JSON data that needs to be integrated:

json_data 
[
  {
    _id: "5af5cf0270d455a211200d4c",
    isActive: true,
    balance: "$3,507.97",
    picture: "http://placehold.it/32x32",
    age: 24,
    eyeColor: "brown",
    name: "Random Name",
    gender: "male",
    company: "AST",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56372522163725227835393b">[email protected]</a>",
    phone: "+1 88888888",
    address: "661 Terrace Place, Elliott, Ohio, 9927",
    about:
      "Id sint labore sint dolore ex laboris. Ea irure dolor est nulla laboris Lorem sint fugiat laborum officia commodo. Reprehenderit culpa non voluptate ea. Fugiat duis et deserunt ea enim et ipsum nostrud commodo quis quis laborum officia. Elit est anim quis deserunt nulla nostrud ea eiusmod quis adipisicing. Mollit exercitation officia ipsum ea aliqua amet aliqua esse amet minim. Ipsum quis cillum fugiat reprehenderit sit aliquip aute in excepteur dolore fugiat esse non non.\r\n",
    registered: "2014-12-10T07:18:10 +02:00",
    latitude: -84.359436,
    longitude: 156.008804,
    tags: [
      "excepteur",
      "eiusmod",
      ...
    ],
    friends: [
      {
        id: 0,
        name: "Shields Terrell"
      },
      {
        id: 1,
        name: "Hilary Bruce"
      },
      ...
    ]
  }

Answer №1

Here is the information:

const employeeData = [
{
  _id: "5af5cf0270d455a211200d4c",
  isActive: true,
  balance: "$3,507.97",
  picture: "http://placehold.it/32x32",
  age: 24,
  eyeColor: "brown",
  name: "Random Name",
  gender: "male",
  company: "AST",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7d6f685c7d6f68327f7371">[email protected]</a>",
  phone: "+1 88888888",
  address: "661 Terrace Place, Elliott, Ohio, 9927",
  about:
    "Id sint labore sint dolore ex laboris. Ea irure dolor est nulla laboris Lorem sint fugiat laborum officia commodo. Reprehenderit culpa non voluptate ea. Fugiat duis et deserunt ea enim et ipsum nostrud commodo quis quis laborum officia. Elit est anim quis deserunt nulla nostrud ea eiusmod quis adipisicing. Mollit exercitation officia ipsum ea aliqua amet aliqua esse amet minim. Ipsum quis cillum fugiat reprehenderit sit aliquip aute in excepteur dolore fugiat esse non non.\r\n",
  registered: "2014-12-10T07:18:10 +02:00",
  latitude: -84.359436,
  longitude: 156.008804,
  tags: ["excepteur", "eiusmod", "laboris", "fugiat", "minim", "dolor", "qui"],
  friends: [
    {
      id: 0,
      name: "Shields Terrell"
    },
    {
      id: 1,
      name: "Hilary Bruce"
    },
    {
      id: 2,
      name: "Lorraine Torres"
    }
  ]
},
{
    _id: "5af5cf0254f91fa2d555e1ae",
    isActive: false,
    balance: "$2,219.42",
    picture: "http://placehold.it/32x32",
    age: 27,
    eyeColor: "blue",
    name: "Ryan Evans",
    gender: "male",
    company: "ast",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8d9cbccf8d9cbcc96dbd7d5">[email protected]</a>",
    phone: "+1 8888888888",
    address: "595 Foster Avenue, Villarreal, Massachusetts, 4604",
    about:
      "Nostrud exercitation ea enim in consequat voluptate sint et laboris laborum elit nisi veniam. Do consectetur magna eiusmod anim nisi id sint consequat. Amet duis proident nisi excepteur. Reprehenderit non amet occaecat deserunt. Duis voluptate non in ex esse sit nostrud esse fugiat laboris fugiat qui reprehenderit.\r\n",
    registered: "2015-07-08T01:24:50 +03:00",
    latitude: -38.471736,
    longitude: -158.491974,
    tags: ["mollit", "minim", "duis", "anim", "aute", "magna", "ut"],
    friends: [
      {
        id: 0,
        name: "Dina Berger"
      },
      {
        id: 1,
        name: "Carmella Mckinney"
      },
      {
        id: 2,
        name: "Campbell Wooten"
      }
    ]
  }
];

document.getElementById("name").innerHTML = `${employeeData[0].name}`;
document.getElementById("fullname").innerHTML = `${employeeData[0].name}`;
document.getElementById("gender").innerHTML = `${employeeData[0].gender}`;
document.getElementById("phone").innerHTML = `${employeeData[0].phone}`;
document.getElementById("company").innerHTML = `${employeeData[0].company}`;
document.getElementById("address").innerHTML = `${employeeData[0].address}`;
document.getElementById("about").innerHTML = `${employeeData[0].about}`;
document.getElementById("registered").innerHTML = `${employeeData[0].registered}`;
h1 {
  text-align: center;
}

ul {
  list-style: none;
}

.flexcontainer {
display: flex;
align-items: center;
}

.container {
padding-top: 1%;
padding-bottom: 1%;
margin-top: 1%;
margin-bottom: 1%;
}
<html>
<head>
<title>Employee Details</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<h1><span id="name"></span></h1>
<div class="container" style="border:1px solid #cecece;">

<div class="flexcontainer">
<div>
<img src="http://placehold.it/350x250" alt="">
</div>
<div>
<ul>
<li>
<h3>Full name: <span id="fullname"></span></h3>
</li>
<li>
<h3>Gender: <span id="gender"></span></h3>
</li>
<li>
<h3>Phone number: <span id="phone"></span></h3>
</li>
<li>
<h3>Company: <span id="company"></span></h3>
</li>
<li>
<h3>Address: <span id="address"></span></h3>
</li>
</ul>
</div>
</div>

<div>
<h3>About employee:</h3>
<p><span id="about"></span></p>
</div>

<div>
<h3>Employee was registered in the system: <span id="registered"></span></h3>
</div>
</div>
</body>

</html>

You can view the demonstration on CodeSandbox here: https://codesandbox.io/s/24n50lmlqy

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

The background color of absolute divs fails to extend across the entire screen

Trying to implement a loading screen in my VueJs app using only divs. The issue is that the background color of the divs (loadingScreen) does not cover the entire screen, but only as much as the loader's height. I've attempted adding margin to th ...

Tips for including parameters in the existing URL using AngularJS

I used the $location.url() method to obtain my current URL. $location.url() returns '/details' Now, I am looking to append some parameters, such as '/details/student' How can I add '/students' to my current URL upon a ...

The primary division containing two inner divisions, one centered and the other positioned to the right

I would like the layout to resemble the image provided. The div header contains two nested divs - the logo div should be centered, and the info div should be positioned to the right with some margin. ...

Is there a way to retrieve attribute values from a JSON object using JMESPath?

I am searching for the value of the _ISSUE_CURRENCY variable. The JSON data I have is as follows: { '#value': 'VR-GROUP PLC', '_ISSUE_CURRENCY': 'EUR', '_PRICING_MULTIPLIER': 1, '_TYPE ...

Creating an adaptable grid system in Vue Material

I am working on a project where I am using Vue-Material to display user-inputted cards in a grid layout. While the cards are rendering correctly, I want to optimize the grid to make it flexible, justify the cards, and stagger them in a way that eliminates ...

Issues with parsing Json data

Here is a JSON file: { "noticiaDTOs": [ { "idiomaPais": { "idioma": { "id": 3, "iso6391": "pt" }, "pais": { "id&quo ...

Extract the complete HTML information from the weather website

Currently, I am attempting to retrieve weather data from the following website: using this code snippet: try { int i = 0; if (googlefirst3.startsWith("http")) { Document document = Jsoup.connect("https ...

Text and Image Coordination

I'm encountering some challenges with a specific section. My goal is for it to resemble this. However, I am currently achieving something different, like this. .timeline { text-align: center; } #about-us { ul { &:before { ...

Guidelines for accessing the data stored in a decoded struct containing an enum type value with customized initializers

Working with an API that returns inconsistent JSON field types, I turned to for assistance and found a model. Here is where I am encountering a problem with the model: struct MyModel: Codable { let id: ID? } enum ID: Codable { case integer(Int) ...

Techniques for adjusting the dimensions of a select dropdown using CSS

Is there a way to control the height of a select dropdown list without changing the view using the size property? ...

Having trouble getting anime.js to function properly in an Ionic 3 project?

I have been attempting to incorporate anime.js into my Ionic 3 project, but I keep encountering an error when using the function anime({}) in the .ts file. Error: Uncaught (in promise): TypeError: __webpack_require__.i(...) is not a function TypeError: _ ...

The "truth" parameter within the xmlHttpRequest .open() function

After referencing MDN, it mentioned that by default, the JavaScript function will keep running until the server response is received. This is essentially what AJAX is all about - Asynchronous JavaScript and XML. Even though I have some experience with A ...

What is the best way to incorporate JSON into my WordPress loop?

I'm looking to optimize my WordPress loop by retrieving JSON data instead of XML. Currently, I have a working solution but it's quite slow: <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <?php $title = $post- ...

The search for the item in JSON by Node JS ProductProvider.getItem is unsuccessful

As a newcomer to JavaScript, I've been scouring the web for an answer without any luck. Coming from a relational database background, I'm having trouble locating a specific record in JSON. This is the JSON snippet I'm working with: Add to ...

Animating the display and concealment of a Ui Bootstrap alert within an AngularJS modal window

Trying to create a dynamic animation for an ng-hide feature used on a Ui Bootstrap alert button within a Ui Bootstrap Modal is proving to be quite challenging. The goal is to have the alert slide in and out while simultaneously fading in and out. Although ...

Transforming a JSON array string into a JSON array results in a null value

Using Alamofire for requests, I encounter a peculiar issue with parsing JSON responses. Here is an example of the JSON string that I receive: Optional("[{\"nl2br\":\"2019-05-02 19:52:10\",\"nl3br\":\"\",\"nl4br ...

What is the best way to implement rate limiting for asynchronous API calls within a specific timeframe?

I have a project that requires me to make over 500 calls simultaneously from my NodeJS server to a third-party server. The issue is that the third-party server has a restriction of only allowing a maximum of 50 calls per second. Can someone assist me in im ...

How can I showcase GWT widgets on RootPanel?

I have a <div> in My.html file: <html><head> <script type="text/javascript" language="javascript" src=" path/biz.path.nocache.js"></script></head> <div id = "div_test"> </div> Now I have created a java cl ...

The use of `await` within a loop may not function as anticipated when the code is being run through Puppeteer, yet it functions flawlessly when executed in the browser's console

Currently, I am assessing the functionality of the codeToBeEvaluated function within a browser environment using puppeteer. Within codeToBeEvaluated, there is a while loop designed to trigger an alert (referenced as LINE B) every 10 seconds. The issue ari ...

"Jackson's @JsonIgnoreProperties failing to function properly when inherited from a parent class

In my project, I have a class called "BaseEntity" from which other entities are extended. Base Entity @MappedSuperclass public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") ...