-4

I am trying to get a response from a API call in python 3.5 and need to filter out the output.

The http response was converted to the dictionary, however, i am not able to apply dictionary functions on it for some reason.

I would like to print key,value from the dictionary using standard builtin functions like result.items(), result.keys() etc

Code:

import os
import json
from xml.etree.ElementTree import ElementTree
import requests
import urllib
import requests
url = 'http://demo.assetexplorer.com/api/cmdb/ci/count/all?OPERATION_NAME=read&TECHNICIAN_KEY=5E28C6CA-CCE2-4C2F-A91D-B37CCA17C629'
response = urllib.request.urlopen(url)
result = json.loads(response.readline().decode('utf-8'))
print(type(result))
print(result)

Output:

<class 'dict'>
{'API': {'response': {'operation': {'name': 'read', 'Details': {'field-values': {'record': {'value': 203}, 'totalRecords': 1}, 'field-names': {'name': {'content': 'Count', 'type': 'Integer'}}}, 'result': {'status': 'Success', 'created-date': 'Jul 28, 2018 03:36 PM', 'statuscode': 200, 'message': 'Successfully fetched.'}}}, 'version': 1}}

The type() does show the variable is a dict, what am i missing here? Image of what i see for the "result" variable output

Atul
  • 130
  • 10
  • 3
    Which functions you can not use? Add it in question... – Marcus.Aurelianus Jul 28 '18 at 08:13
  • @Marcus.Aurelianus edited the question, thank you – Atul Jul 28 '18 at 08:21
  • If it's a dict, you can use dict functions on it. If you can't use dict functions on it, it's not a dict. Please post a [mcve]. – Aran-Fey Jul 28 '18 at 08:32
  • So did you try e.g. `result.items()`? What happened? Give a [mcve] (for example, it seems irrelevant how you *get* the dictionary if the problem is what you're doing with it). – jonrsharpe Jul 28 '18 at 08:32
  • @jonrsharpe i did, i was not able to get any of the sub functions to pop up in my pycharm for the result variable. Interestingly it works for other dictionary variables. Is there a way the http response is not getting stored in a dictionary like other dictionary variables ? – Atul Jul 28 '18 at 08:35
  • 2
    If I execute your code I get "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" at line 10. Is it working for you ? – joaquin Jul 28 '18 at 08:38
  • @joaquin i have masked the actual URL, it contained sensitive information. I am able to get valid response ( check the output in my question ). I am trying to filter out the output and hence need to apply dictionary functions on the "result" variable – Atul Jul 28 '18 at 08:41
  • Please extend your code example with the lines of code you try to run and the output you get. What your pycharm sees maybe is different than what you actually have – joaquin Jul 28 '18 at 08:45
  • @joaquin i have added the actual url for trial. Try using dictionary functions on the "result" variable ( this stores the response from API ) – Atul Jul 28 '18 at 08:50
  • 1
    Your code works perfectly. You can apply any dict function you like – joaquin Jul 28 '18 at 08:50
  • And I will really appreciate if people can be a little patient and stop hitting the down arrow on the question for nothing – Atul Jul 28 '18 at 08:51
  • @joaquin check the image i have added in question, i don't get the functions for "result" variable, unlike other dictionary variables. – Atul Jul 28 '18 at 08:57
  • 3
    forget what you editor sees !. Just apply the function – joaquin Jul 28 '18 at 08:59
  • Just apply the function. Python knows it is a dict. Pycharm is not perfect. The variable came from 'outside' so Pycharm probably cannot infer it is a dictionary – joaquin Jul 28 '18 at 09:07
  • @joaquin That is interesting, you are correct. The editor is being fooled by this, i am able to filter out the results. Thank you so much for sticking along and helping out. Appreciate it! For anyone who has similar issue,see the workarout `print(result['API']['response']['operation']['result']['status'])` – Atul Jul 28 '18 at 09:09

1 Answers1

0

I tried with the python 3.6. with cleanup to your code and both test_dict.items() and test_dict.keys() are working. First thing is http://www.example.com did not provide json so I used 'https://jsonplaceholder.typicode.com/posts/1'

Second thing is using urllib with decoder gave me error "json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:" So modified it based on this stackoverflow post

Here is the working code

import json
import requests

url = 'https://jsonplaceholder.typicode.com/posts/1'
test_value = requests.get(url).json()
print(test_value)
print(test_value.items())
print(test_value.keys())

Here is the output

{'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}

dict_items([('userId', 1), ('id', 1), ('title', 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'), ('body', 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto')])

dict_keys(['userId', 'id', 'title', 'body'])

kadekhar
  • 71
  • 3