-1

In running the following code, I am interested in the "Report_ForecastIncomeSummary" dictionary. However, I have not yet been able to figure out how to use that dictionary. I apologize for my ignorance and perhaps I am approaching this incorrectly.

My Code:

import requests
import json

# Note, the http://x.x.x.x:4500/api/ezapp/getdata bit in my code 
# does contain the correct IP and port number. It has been masked here.

ezAPI = 'http://x.x.x.x:4500/api/ezapp/getdata'
myData = {'Request': "{'Request':'Report_ForecastIncomeSummary','DateFrom':'1/3/2020','DateThru':'1/3/2020'}"}
response = requests.post(ezAPI, json=myData)

data = json.loads(response)

Response:

{"Success":true,"FailureInformation":"","Result":"{\"Request\": \"Report_ForecastIncomeSummary\", \r\n\"status\": \"OK\",\r\n\"Report_ForecastIncomeSummary\": [\r\n{\"TicketState\":\"future\",\"AmountScheduled\":\"144.45\",\"AmountUnscheduled\":\"0\",\"amount\":\"144.45\"},\r\n{\"TicketState\":\"invoiced\",\"AmountScheduled\":\"0\",\"AmountUnscheduled\":\"380\",\"amount\":\"380\"},\r\n{\"TicketState\":\"ticket\",\"AmountScheduled\":\"3846.73\",\"AmountUnscheduled\":\"401\",\"amount\":\"4247.73\"}\r\n] }"}

Also, I can't figure out why the escape characters are returned ... not sure if that's part of my problem.

Again, the part of this response I want to store as a dictionary is:

"Report_ForecastIncomeSummary\": [\r\n{\"TicketState\":\"future\",\"AmountScheduled\":\"144.45\",\"AmountUnscheduled\":\"0\",\"amount\":\"144.45\"},\r\n{\"TicketState\":\"invoiced\",\"AmountScheduled\":\"0\",\"AmountUnscheduled\":\"380\",\"amount\":\"380\"},\r\n{\"TicketState\":\"ticket\",\"AmountScheduled\":\"3846.73\",\"AmountUnscheduled\":\"401\",\"amount\":\"4247.73\"}\r\n]

Appreciate any assistance or guidance. I am not using flask here just python locally, aside from the API call. Also, in using Stackoverflow as a resource over the years I know there is a lot of focus on researching it first and articulating the issue correctly. I apologize in advance if I broke any cardinal rules. Thank you.

Rajnish
  • 1,136
  • 3
  • 10
  • 26
Brian Samson
  • 60
  • 1
  • 8

1 Answers1

0

requests comes with an amazing feature .json() :

You can just write :

response = requests.post(ezAPI, json=myData).json()

And you'll get a python dic

Maxime
  • 796
  • 4
  • 15
  • Thank you. However, the issue is accessing this portion of the response: "Report_ForecastIncomeSummary\": [\r\n{\"TicketState\":\"future\",\"AmountScheduled\":\"144.45\",\"AmountUnscheduled\":\"0\",\"amount\":\"144.45\"},\r\n{\"TicketState\":\"invoiced\",\"AmountScheduled\":\"0\",\"AmountUnscheduled\":\"380\",\"amount\":\"380\"},\r\n{\"TicketState\":\"ticket\",\"AmountScheduled\":\"3846.73\",\"AmountUnscheduled\":\"401\",\"amount\":\"4247.73\"}\r\n] – Brian Samson Jan 06 '20 at 14:55
  • You can access the nested keys in a dictionnary by using my_dict[key1][key2][...][key_n] – Maxime Jan 06 '20 at 14:57