I am trying to access the value for a field that is part of an array within JSON data. Here is the JSON Data (I :
{
"response":
{
"test-item": {
"id": 125252,
"code": null,
"name": "test_1",
"section_id": 85552653,
"state": "active",
"start_date": "2016-10-10 00:00:00",
"end_date": null,
"timezone": "US/Pacific",
"discrepancy_pct": 0,
"publishers_allowed": "all",
"campaigns": [
{
"id": 85669995691,
"name": "test_campaign",
"profile_id": 43562,
"inventory_type": "direct",
"state": "active",
"priority": 5,
},
{
"id": 800099981123,
"name": "test_campaign_2",
"profile_id": 12562,
"inventory_type": "direct",
"state": "active",
"priority": 5,
}
]}}}
I am only trying to pull the values for the fields "id" which appears in the "campaigns" array. Here is what the section of my code that I am using to accomplish this looks like:
url = "https://api.example.com"
header = {"Authorization": authorization_code}
api_response = requests.get(url, headers=header)
test_response = json.loads(api_response.text)
filtered_campaigns = test_response.get("response", {}).get("test-item",
{}).get("campaigns", "id")
When using this, I get back the entire value for the "campaigns" array instead of just the ids for all of the campaigns. Can anyone help point out what I am doing wrong here? I am still fairly new to Python.