pakwai122
pakwai122

Reputation: 67

API return issue in Pandas with JSON

I have a file for input parameter and request every row in the input column and get the first item back, then export as a csv file. My question is, the code works fine, but when the input para with special character like 'abc & bbc'. It will return the second item from the JSON. How can I fix it to prevent such an issue?

import pandas as pd
import requests
import json

def getSearchReturn(row):
    para = row['input']
    url = f"http://localhost/product?cat={para}"
    try:
        # get the first response string
        return_json = requests.get(url).json()
        data = return_json['data']
        product_ID = data[0]['product_id']
        product_Name = data[0]['product_name']

    except:
        # set a value for the case that the API call has no correct results
        product_ID = None
        product_Name = None

    row['productID'] = product_id
    row['productName'] =product_name

    return row


data = pd.read_csv("input.csv") 
data = data.apply(getSearchReturn, axis =1)
data.to_csv("output.csv",index=False)

The following is a json return from the API

{
    "data": [
        {"product_id": "1", "product_name": "book"},
        {"product_id": "2", "product_name": "car"}
    ]
}

Upvotes: 1

Views: 59

Answers (2)

azro
azro

Reputation: 54148

Fix

The value may be encoded, for the space/ampersand to be safe send, using urllib for example

from urllib.parse import quote

print(quote("abc&bbc"))  # abc%26bbc
print(quote("abc & bbc"))  # abc%20%26%20bbc


requests.get("http://localhost/product?cat=" + quote(para))

Improve

Use the params argument of requests.get

url = "http://localhost/product"
return_json = requests.get(url, params={'cat': para}).json()

Upvotes: 1

aksh02
aksh02

Reputation: 298

'&' separates queries in url parameters. if you want to query for both 'abc' and 'bbc', you url should be "http://localhost/product?cat=abc&cat=bbc" but instead it is f"http://localhost/product?cat=abc & bbc"

Upvotes: 0

Related Questions