Home > Net >  How should I code the situation to get data with json
How should I code the situation to get data with json

Time:12-20

Suppose you are writing a python web client to access an API of an online supermarket. Given below are the API details.

Base URL= http://host1.open.uom.lk:8080

Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.

Hint: the json response will be of the following example format:

{
    "message": "success",
    "data": [
        {
            "id": 85,
            "productName": "Araliya Basmathi Rice",
            "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
            "category": "Rice",
            "brand": "CIC",
            "expiredDate": "2023.05.04",
            "manufacturedDate": "2022.02.20",
            "batchNumber": 324567,
            "unitPrice": 1020,
            "quantity": 200,
            "createdDate": "2022.02.24"
        },
        {
            "id": 86,
            "productName": "Araliya Basmathi Rice",
            "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.",
            "category": "Rice",
            "brand": "CIC",
            "expiredDate": "2023.05.04",
            "manufacturedDate": "2022.02.20",
            "batchNumber": 324567,
            "unitPrice": 1020,
            "quantity": 200,
            "createdDate": "2022.02.24"
        }
    ]
}

Hi guys I'm stuck in a question based on python Web-scraping and i've been stuck in this question for more than one month. Here's the codes I tried, I still can't think of what my mistake is...

1.

import requests
import json
BASE_URL= 'http://host1.open.uom.lk:8080'
response= requests.get(f"{BASE_URL}/api/products")
y=json.dumps(response,indent=5,sort_keys=True)
print(y)
import requests
import json
BASE_URL= 'http://host1.open.uom.lk:8080'
response= requests.get(f"{BASE_URL}/api/products")
print(response.json())

CodePudding user response:

Althougth your provided API endpoints do not seem to work i will try to provide a broader example for you to play around with via the free api "CatFacts"

url = "https://catfact.ninja/facts"
import requests
import json
response= requests.get(url)
data = response.json()
print(json.dumps(response.json(),indent=4))
print(len(data["data"]))

Above we are accessing the "CatFacts" API and printing all the data that is retrieved from us. You will notice when you run this code your self (or go to https://catfact.ninja/facts) that there is a piece of the JSON returned called data that holds a list of cat facts. To find out how many cat facts we have we just call len() on the list of cat facts from our returned JSON object

Hopefully with this example you are able to get yours to work

CodePudding user response:

Regarding the sample data you provided, I assume that you need to calculate the sum of the quantity of each product to get the total number of products currently stored on the server

You can convert the json response to dictionary via json.load() function and iterate through its data

import requests
import json

BASE_URL= 'http://host1.open.uom.lk:8080'
response= requests.get(f"{BASE_URL}/api/products")

products = json.load(response)
total=0

for product in products['data']:
    total  = product['quantity']
    
print(total)
  • Related