What are the common data formats returned by APIs and how do you handle them?

APIs (Application Programming Interfaces) commonly return data in various formats, with JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) being the most prevalent. Other formats may include CSV (Comma-Separated Values), HTML (HyperText Markup Language), and plain text. Here is how you can handle each of these data formats:

JSON

JSON is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. JSON is often used in REST APIs.

Handling JSON in Python:

import requests

# Make a request to an API
response = requests.get("https://api.example.com/data")

# Parse JSON response
data = response.json()

# Access data
print(data["key"])

Handling JSON in JavaScript (using fetch):

// Make a request to an API
fetch("https://api.example.com/data")
  .then(response => response.json()) // Parse JSON response
  .then(data => {
    // Access data
    console.log(data.key);
  });

XML

XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.

Handling XML in Python (using ElementTree):

import requests
import xml.etree.ElementTree as ET

# Make a request to an API
response = requests.get("https://api.example.com/data.xml")

# Parse XML response
root = ET.fromstring(response.content)

# Access data
for child in root:
    print(child.tag, child.text)

Handling XML in JavaScript (using DOMParser):

// Make a request to an API
fetch("https://api.example.com/data.xml")
  .then(response => response.text()) // Get the response text
  .then(str => {
    // Parse XML from a string
    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(str, "text/xml");

    // Access data
    const elements = xmlDoc.getElementsByTagName("tagname");
    for (let i = 0; i < elements.length; i++) {
      console.log(elements[i].textContent);
    }
  });

CSV

CSV is a simple file format used to store tabular data, such as a spreadsheet or database.

Handling CSV in Python:

import requests
import csv
import io

# Make a request to an API
response = requests.get("https://api.example.com/data.csv")

# Parse CSV response
csv_file = io.StringIO(response.text)
csv_reader = csv.reader(csv_file)

# Access data
for row in csv_reader:
    print(row)

Handling CSV in JavaScript:

JavaScript does not have a built-in CSV parser, but you can manually parse the CSV data or use a library like PapaParse.

// Make a request to an API
fetch("https://api.example.com/data.csv")
  .then(response => response.text()) // Get the response text
  .then(csvString => {
    // Parse CSV string
    const rows = csvString.split("\n").map(row => row.split(","));

    // Access data
    rows.forEach(row => console.log(row));
  });

HTML

HTML is the standard markup language for creating web pages and web applications.

Handling HTML in Python (using BeautifulSoup):

import requests
from bs4 import BeautifulSoup

# Make a request to an API or web page
response = requests.get("https://api.example.com/data")

# Parse HTML response
soup = BeautifulSoup(response.text, 'html.parser')

# Access data
for link in soup.find_all('a'):
    print(link.get('href'))

Plain Text

Plain text data can be simply read as is, without the need for specific parsing.

Handling Plain Text in Python:

import requests

# Make a request to an API
response = requests.get("https://api.example.com/data.txt")

# Access data
print(response.text)

Handling Plain Text in JavaScript:

// Make a request to an API
fetch("https://api.example.com/data.txt")
  .then(response => response.text()) // Get the response text
  .then(text => {
    // Access data
    console.log(text);
  });

When working with APIs, it's essential to check the documentation to understand the data format returned. This will ensure that you can correctly parse and handle the data in your applications.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon