Home > Software design >  How to extract a couple of fields nested in response using python
How to extract a couple of fields nested in response using python

Time:04-25

I'm a python beginner. I would like to ask for help regarding the retrieve the response data. Here's my script:

import pandas as pd
import re
import time
import requests as re
import json

response = re.get(url, headers=headers, auth=auth)
data = response.json()

Here's a part of json response:

{'result': [{'display': '',
   'closure_code': '',
   'service_offer': 'Integration Platforms',
   'updated_on': '2022-04-23 09:05:53',
   'urgency': '2',
   'business_service': 'Operations',
   'updated_by': 'serviceaccount45',
   'description': 'ALERT returned 400 but expected 200',
   'sys_created_on': '2022-04-23 09:05:53',
   'sys_created_by': 'serviceaccount45',
   'subcategory': 'Integration',
   'contact_type': 'Email',
   'problem_type': 'Design: Availability',
   'caller_id': '',
   'action': 'create',
   'company': 'aaaa',
   'priority': '3',
   'status': '1',
   'opened': 'smith.j',
   'assigned_to': 'doe.j',
   'number': '123456',
   'group': 'blabla',
   'impact': '2',
   'category': 'Business Application & Databases',
   'caused_by_change': '',
   'location': 'All Locations',
   'configuration_item': 'Monitor',
   },

I would like to extract the data only for one group = 'blablabla'. Then I would like to extract fields such as:

number = data['number']
group = data['group']
service_offer = data['service_offer']
updated = data['updated_on']
urgency = data['urgency']
username = data['created_by']
short_desc = data['description']

How it should be done?

I know that to check the first value I should use:

service_offer = data['result'][0]['service_offer']

I've tried to create a dictionary, but, I'm getting an error:

 data_result = response.json()['result']

payload ={
    number = data_result['number']
    group = data_result['group']
    service_offer = data_result['service_offer']
    updated = data_result['updated_on']
    urgency = data_result['urgency']
    username = data_result['created_by']
    short_desc = data_result['description']
}

TypeError: list indices must be integers or slices, not str:

So, I've started to create something like below., but I'm stuck:

get_data = []
if len(data) > 0:
      for item in range(len(data)):
        get_data.append(data[item])

May I ask for help?

CodePudding user response:

If data is your decoded json response from the question then you can do:

# find group `blabla` in result:
g = next(d for d in data["result"] if d["group"] == "blabla")

# get data from the `blabla` group:
number = g["number"]
group = g["group"]
service_offer = g["service_offer"]
updated = g["updated_on"]
urgency = g["urgency"]
username = g["sys_created_by"]
short_desc = g["description"]

print(number, group, service_offer, updated, urgency, username, short_desc)

Prints:

123456 blabla Integration Platforms 2022-04-23 09:05:53 2 serviceaccount45 ALERT returned 400 but expected 200
  • Related