-1

Here is my code:

import requests
import json 


url = 'https://....'
request = requests.get(url)

request_text = request.text


data = json.loads(request_text)
data_serialized = json.dump(data , open('mainapp.json' ,"w") , indent= 4)

The JSON format of the file is:

{
"agent_timeline": [{
        "agent_id": 394903921554,
        "engagement_count": 0,
        "start_time": "2020-06-15T06:00:00.000000Z",
        "status": "invisible",
        "duration": 901.929878
    },
    {
        "agent_id": 397330457313,
        "engagement_count": 0,
        "start_time": "2020-06-15T06:00:00.000000Z",
        "status": "invisible",
        "duration": 901.929878
    },
    {
        "agent_id": 401565578994,
        "engagement_count": 0,
        "start_time": "2020-06-15T06:00:00.000000Z",
        "status": "invisible",
        "duration": 4.664595
    },
    {
        "agent_id": 394903921554,
        "engagement_count": 0,
        "start_time": "2020-06-15T06:00:11.000000Z",
        "status": "online",
        "duration": 901.929878
    }
]}

How can I get the results for a specific agent? For example how can I get the results for agent id 394903921554 only?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    nit: you could do `data = request.json()` – Marat Jun 17 '20 at 16:29
  • Does this answer your question? [Accessing elements of Python dictionary by index](https://stackoverflow.com/questions/5404665/accessing-elements-of-python-dictionary-by-index) – mkrieger1 Jun 17 '20 at 16:51

3 Answers3

0

json.loads converts json data into python objects according to the following table from https://docs.python.org/3/library/json.html#json-to-py-table:

object -> dict

array -> list

string -> str

number (int) -> int

number (real) -> float

true -> True

false -> False

null -> None

In your case, you had a json "object", so you can operate on it like a python dictionary. Following the structure of the json in your example, data["agent_timeline"] would give you a list of dictionaries, where each element is an agent, and you would have to iterate over that to find the specific agent you want. For example:

for agent in data["agent_timeline"]:
  if agent["agent_id"] == 394903921554:
    print(agent)
Cz_
  • 371
  • 2
  • 8
0

requests has a built-in "convert to json" function so in my example below I use that. Then you can use the next() function to find the next occurrence of an agent with the ID you specified.

import requests

url = 'https://....'
response = requests.get(url)
data = response.json()
agents = [item for item in data['agent_timeline'] if item['agent_id'] == 394903921554]

Edit: Changed the result to return multiple agents instead of just the first one.

chrislondon
  • 12,487
  • 5
  • 26
  • 65
0

Maybe something like this:

data = json.loads(s)
filtered_data = [element for element in data['agent_timeline'] if element['agent_id']==394903921554]
print(filtered_data)

Output:

[{'agent_id': 394903921554, 'engagement_count': 0, 'start_time': '2020-06-15T06:00:00.000000Z', 'status': 'invisible', 'duration': 901.929878}, {'agent_id': 394903921554, 'engagement_count': 0, 'start_time': '2020-06-15T06:00:11.000000Z', 'status': 'online', 'duration': 901.929878}]
PApostol
  • 2,152
  • 2
  • 11
  • 21