Posted by RlS
Trigon

Get Device Report JSON into python dictionary

Hello All

I have a Python script that successfully dopwnloads the devices report from
https://webapi.teamviewer.com/api/v1/devices

My problem begins when I try to get at individual fields in the repport theough a python dictionary; for example I want the vlaues of the remotecontrol_id, device_id, alias etc to go eventually into SQL Server. This is proving impossible as all these appear to be located as one huge string under the devices key. Is there anyway to do this?

Thanks

.

 

10 Replies
2 Accepted Solutions

Accepted Solutions
Posted by DomLan
Heptagon
Solution

Re: Get Device Report JSON into python dictionary

Hi @RlS 

try like this:

import json
dati = '{"devices": [{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "PC","groupid": "g12345678","online_state": "Online","supported_features": "remote_control chat","policy_id": "5d93f008-3b1f-8472-8555-3549d5a68092"},{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "Laptop","groupid": "g12345678","online_state": "Offline","supported_features": "remote_control"},{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "Office","groupid": "g12345678","online_state": "Offline","assigned_to": "True","last_seen": "2017-11-28T11:08:31Z"}]}'


all = json.loads(dati)

print all['devices'][0]['device_id']
print all['devices'][1]['device_id']
print all['devices'][2]['device_id']
Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon
Solution

Re: Get Device Report JSON into python dictionary

Hooray! that's getting it when I drop ...v1/reports/devices  What is that for, seems to be an error in the documentation?

Thanks So Much!!!

10 Replies
Posted by DomLan
Heptagon
Solution

Re: Get Device Report JSON into python dictionary

Hi @RlS 

try like this:

import json
dati = '{"devices": [{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "PC","groupid": "g12345678","online_state": "Online","supported_features": "remote_control chat","policy_id": "5d93f008-3b1f-8472-8555-3549d5a68092"},{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "Laptop","groupid": "g12345678","online_state": "Offline","supported_features": "remote_control"},{"remotecontrol_id": "r**Please do not post TeamViewer IDs**","device_id": "d**Please do not post TeamViewer IDs**","alias": "Office","groupid": "g12345678","online_state": "Offline","assigned_to": "True","last_seen": "2017-11-28T11:08:31Z"}]}'


all = json.loads(dati)

print all['devices'][0]['device_id']
print all['devices'][1]['device_id']
print all['devices'][2]['device_id']
Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon

Re: Get Device Report JSON into python dictionary

 Hi DomLan

Thanks

Now getting - TypeError: the JSON object must be str, bytes or bytearray, not Response

 

oh yes also the URL of the REST thing I originally put down is incorecct I am now using;

webapi.teamviewer.com/api/v1/reports/devices"

 

TypeError: the JSON object must be str, bytes or bytearray, not Response

 

Im trying this

 

headers = {"content-type": "application/json", "Authorization": "Bearer XXXXX"}
    response = requests.get(url, headers=headers)     
    all = json.loads(response)    
    for k,v in all.items():    
        print(r3['records'][0]['id'])
        print(r3['records'][1]['originid'])
        print(r3['records'][2]['userid'])

 

 

 

Any ideas?

Posted by DomLan
Heptagon

Re: Get Device Report JSON into python dictionary

Hi @RlS 

for JSON Response Content there's also a builtin JSON decoder, so

import requests

headers = {"content-type": "application/json", "Authorization": "Bearer XXXXX"}
response = requests.get(url, headers=headers)
all = response.json()
for k,v in all.items():
print(r3['records'][0]['id'])
print(r3['records'][1]['originid'])
print(r3['records'][2]['userid'])

 

Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon

Re: Get Device Report JSON into python dictionary

Hi DornLan

headers = {"content-type": "application/json", "Authorization": "Bearer "xxxx"}
response = requests.get(url, headers=headers)
all = response.json()
Throws:
print(all['records'][0]['id'])
KeyError: 'id'

It can't see 'id'. It looks as if there is only one key 'records' all the others are in one long string under the records key? Was this planned? why would teramviewer do thid? has anybody got this to work in python 3.7, or should I be using something other language?

thanks

Posted by DomLan
Heptagon

Re: Get Device Report JSON into python dictionary

Sorry, I've copied all from previous reply.

Root is devices and field device_id -> print all['devices'][0]['device_id']

Try here!

Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon
Solution

Re: Get Device Report JSON into python dictionary

Hooray! that's getting it when I drop ...v1/reports/devices  What is that for, seems to be an error in the documentation?

Thanks So Much!!!

Posted by DomLan
Heptagon

Re: Get Device Report JSON into python dictionary

Hi @RlS 

I don't think so: I feel I can say, as a user, that this is one of the most accurate documentation I've seen over time

DocTeamViewerDevices01.pngDocTeamViewerDevices02.png

You are probably referring to problems of homogeneity in the definition of the type of data returned: yes, there are some. But technically they are all justifiable, somehow, and I don't think it's worth reporting them. In any case, a change at this level would generate a breaking-change that is difficult to justify.

The documentation of every single call and its answer must always be kept in hand.

Regards

Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon

Re: Get Device Report JSON into python dictionary

Yes, I was just confused by the reference to /reports in the URL. Anyway, when you run this do you get all the devices in the JSON or just one line at a time wirhthat code. All I'm getting is one line at a time?

Posted by DomLan
Heptagon

Re: Get Device Report JSON into python dictionary

Hi @RlS,

If you agree, I would say that your initial question has been resolved. I believe it is correct to follow the response validation mechanism, once your query has found a correct solution or at least your initial problem has been solved.

In your case it also applies to other threads you have started on the same topic, which have been answered, but we don't know if they helped you solve your problem.

You have started this thread by saying: "I have a Python script that successfully dopwnloads the devices report from https://webapi.teamviewer.com/api/v1/devices"

What I can understand is:
- you have no problem
- you know you are working with an array of results (if you get only one it's because you set a filter on request)
- the term /report in the address has never been mentioned.

In order that it does not become a conversation limited to a specific case that deals with potentially different problems, I suggest you start a new thread, maybe more specific and in which you provide a greater number of details about the lines of code you use (obviously you don't have to report codes that allow others to trace your authentication! remains generic)

I think a good way to solve the problem you're encountering is to divide it into smaller problems ... and in any case, the first answer that was given to you in this thread I think was the correct one.

Regards

Domenico Langone
MCSD: App Builder
Posted by RlS
Trigon

Re: Get Device Report JSON into python dictionary

Thank you, I will start a new thread