print the most recent “timestamp_measured” and concentration values from an api in python

You can use the requests module to make an HTTP request to the API, and parse the response using the json module. Here's an example code snippet that prints the most recent timestamp_measured and concentration values from the API:

main.py
import requests
import json

api_url = 'https://api.example.com/data'

response = requests.get(api_url)

if response.status_code == 200:
    data = json.loads(response.content)
    if data:
        measurements = data.get('measurements')
        if measurements:
            most_recent_measurement = measurements[-1]
            timestamp_measured = most_recent_measurement.get('timestamp_measured')
            concentration = most_recent_measurement.get('concentration')
            print(f"Most recent timestamp measured: {timestamp_measured}")
            print(f"Concentration value: {concentration}")
    else:
        print("No data found in response from API")
else:
    print(f"Error getting data from API. Status code: {response.status_code}")
755 chars
22 lines

Replace api_url with the URL of the API you're using. The code checks if the response from the API is valid, and if so, extracts the measurements from the response. It then gets the most recent measurement and prints the values of timestamp_measured and concentration.

related categories

gistlibby LogSnag