101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    API Interaction

    Working with JSON

    text-based open standard designed for human-readable data interchange

    Text-based open standard designed for human-readable data interchange.

    JavaScript Object Notation, or JSON, 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 based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

    JSON Syntax

    JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.

    Here's a simple example of JSON data:

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

    In JSON, keys must be strings, written with double quotes, and values must be a valid JSON data type (string, number, object, array, boolean or null). Objects are enclosed in curly braces {} and key-value pairs are separated by a colon :. Arrays are enclosed in square brackets [] and values are separated by a comma ,.

    Parsing JSON Data

    When receiving data from a web server, the data is always a string. To convert a JSON text into a JavaScript object, you can use the JSON.parse() method. Here's an example:

    let text = '{"name":"John", "age":30, "city":"New York"}'; let obj = JSON.parse(text); console.log(obj.name); // Outputs: John

    Stringifying JavaScript Objects to JSON

    If you have a JavaScript object and you want to convert it into a JSON string, you can use the JSON.stringify() method. Here's an example:

    let obj = {name: "John", age: 30, city: "New York"}; let json = JSON.stringify(obj); console.log(json); // Outputs: {"name":"John","age":30,"city":"New York"}

    Working with JSON Data from APIs

    When you're working with APIs, you'll often receive data in JSON format. Here's an example of how you can fetch JSON data from an API and parse it:

    fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error));

    In this example, response.json() returns a promise that resolves with the result of parsing the body text as JSON, which can be used in the next then() block.

    Understanding how to work with JSON is crucial for any web developer, as it's the most common format for sending and receiving data from web APIs.

    Test me
    Practical exercise
    Further reading

    Buenos dias, any questions for me?

    Sign in to chat
    Next up: Console methods