WebDev Beginner
  • Welcome to TechAcademy
  • Project
    • Project Introduction
    • Minimal Requirements
    • More Ideas
    • Curriculum
  • Video Plan
    • Youtube
  • Web Development Fundamentals
    • How to use this section?
    • 2. HTML
    • 3. CSS
    • 4. JavaScript
    • 5. Interaction HTML, CSS and JavaScript
  • Tools
    • IDE - Visual Studio Code
    • API
  • ADDITIONAL RESOURCES
    • Git & GitHub
    • JSON
    • Font Awesome
Powered by GitBook
On this page
  1. ADDITIONAL RESOURCES

JSON

JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is widely used in web applications for transmitting data between servers and clients.

JSON is a text format that is completely language independent, making it easy to use with almost any programming language. It is based on a subset of the JavaScript programming language, but it can be used with any programming language that supports object notation.

JSON consists of two main structures: objects and arrays. An object is an unordered collection of key/value pairs, where the keys are strings and the values can be any JSON data type, including objects and arrays. An array is an ordered collection of values, which can also be any JSON data type, including objects and arrays.

Here's an example of a simple JSON object:

Copy

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

This JSON object has three key/value pairs: "name" with a string value of "John Doe", "age" with a numeric value of 30, and "city" with a string value of "New York".

JSON is commonly used in web APIs to send and receive data between servers and clients. For example, a server might send a JSON object containing a list of songs to a client, and the client would parse the JSON object to display the blog posts in a user interface.

Parsing means analyzing a piece of text or code to understand its structure and meaning. It's like breaking down a sentence to understand what it means or checking if a piece of code is written correctly.

Overall, JSON is a versatile and widely used data format that is easy to work with and supports a wide range of programming languages and applications.

How do I read the JSON data in JavaScript

To use JSON data from an API inside a JavaScript function, you would typically use the fetch() method to make a request to the API, and then parse the JSON data returned by the API using the JSON.parse() method. Here is an example of how this can be done:

Copy

async function getData() {
  try {
    const response = await fetch('https://example.com/api/data');
    const data = await response.json();
    // Use the JSON data in your function
    console.log(data);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

In this example, the async keyword is used to declare the function as an asynchronous function. The await keyword is then used to wait for the fetch() and response.json() methods to return their respective values before assigning them to the response and data variables. The variable data contains now an object that has all the data from the endpoint.

PreviousGit & GitHubNextFont Awesome