Contents

About understanding data formats in APIs

   Jan 30, 2024     2 min read

outline

Application programming interfaces (APIs) are a key element for seamlessly exchanging data between software applications.

Understanding data formats is very important when working with APIs.

In this article, we’ll look at the differences between strings commonly used in API interactions and JavaScript Object Notation (JSON).

String: Basic text data

A string represents text data as a sequence of characters.

It is one of the fundamental data types in most programming languages.

APIs often transmit data over a network in string format.

This is because the network works more efficiently with text data.

JSON: Lightweight data exchange

JSON, on the other hand, is a lightweight data exchange format.

It can be easily read and written by both people and machines.

JSON provides a structured way to represent data objects as attribute-value pairs and is well suited for data exchange.

Why use strings in API responses?

When an API responds to a request, it usually returns data in string format.

This is due to the nature of network communication.

Strings are more efficient to transmit over networks and provide a universal format for reading data.

JSON parsing: Convert string to object

When you receive an API response in string format, you typically need to convert it into a format usable by your programming language.

In JavaScript, you use the JSON.parse() function to convert a JSON string to a JavaScript object or array.

This parsing step allows you to utilize your data effectively.

Real-life example

Let’s consider an API that returns financial market data.

There may be cases where it returns daily candlestick information for a cryptocurrency such as Bitcoin (BTC).

The response will be a JSON string containing details such as open price, close price, high price, low price, volume and timestamp for multiple dates.

const apiResponse =
  '[{"market":"KRW-BTC","candle_date_time_utc":"2023-11-15T00:00:00", ... }]';

const parsedData = JSON.parse(apiResponse);
console.log(parsedData);

In the example above, parsedData now holds the API response as a JavaScript object, allowing developers to easily access specific data fields such as trade_price.

conclusion

Understanding the interaction between strings and JSON in API interactions is essential for effective data exchange and utilization.

If you’re developing an application that uses an API or designing an API, being aware of the role these data types play will help you work seamlessly within the broader software ecosystem.