Displaying Decimal Places in JSON Data with Pandas
When you use the json_load method from pandas to parse a JSON feed, there may be a limitation when dealing with decimal points. The problem arises because some APIs return data with commas as thousands separators, while others use periods (.) instead.
Here is an article on how to display all decimal numbers coming from a JSON feed when you use pandas to convert the data:
Problem Statement
When using the json_load parameter to parse a JSON feed that contains decimal values, such as stock prices or financial data, you may encounter problems displaying these numbers correctly. Specifically, the code tries to handle cases where the API returns data with commas (,) as thousands separators.
Solution: Using pd.read_json() with parse_dates=True

To solve this problem, we can use the pandas.read_json() function along with the parse_dates=True parameter. This approach allows us to parse JSON files in a pandas dataframe without losing data accuracy.
import pandas as pd
Replace " with your actual API endpoint URLurl = '
def fetch_json_data(url, parameters):
"""
Retrieve JSON data from the specified URL and return it in a pandas data frame.
Parameters:
url (str): API endpoint URL.
params (dict): Query parameter dictionary to filter the response.
Returns:
pd.DataFrame: DataFrame containing the retrieved JSON data.
"""
Set the API request parametersparams = {k: v for k, v in params.items() if k is not in ['timestamp', 'open', 'high', 'low', 'close']}
Get JSON data using built-in function read_json() pandasdata = pd.read_json(url, params=params)
return data
def main():
url = '
params = {'symbol': 'BTCUSDT', 'interval': '1m', 'limit': 1000}
Adjust these parameters to your needsdata = fetch_json_data(url, params)
print(data)
if __name__ == '__main__':
main()
How it works:
The fetch_json_data() function takes as input the URL of the API endpoint and the query parameters. First, we convert the query parameters into a dictionary with only non-numeric keys (timestamp, open, high, low, close), which are useful for filtering the response.
We then use pd.read_json() to read the JSON data from the specified URL using these filtered parameters. The resulting DataFrame is then printed to the console.
Tips and Variations:
- You can customize the API request parameters to suit your specific needs.
- Be aware of API rate limits to avoid excessive requests.
- If you are dealing with large datasets, consider breaking the JSON data into smaller chunks using
chunksize=10000.
- To display decimal values as floating points instead of integers, simply change
int()tofloat()in the pandas dataframe.
