Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Rest Python

Tags:

python

rest

I'm working with Python API Rest between Django and LogicalDOC. I want to display list of directories inside one directory which have this parent ID : 8552450

I'm using this documentation : http://wiki.logicaldoc.com/rest/#/

So I have this command :

url = 'http://demoged.fr:8009/services/rest/folder/listChildren?folderId=8552450'
payload = {'folderId': 8552450}

headers = {'Accept': 'application/json'}
r = requests.get(url,  params=payload, headers=headers, auth=('***', '***'))

rbody = r.content
print rbody

And I get :

[{"id":8978437,"name":"LKJLKJ_OKJKJ_1900-09-12","parentId":8552450,"description":"","lastModified":"2017-02-06 14:45:40 +0100","type":0,"templateId":null,"templateLocked":0,"creation":"2017-02-06 14:45:40 +0100","creator":"Etat Civil","position":1,"hidden":0,"foldRef":null,"attributes":[],"storage":null,"tags":[]}]

Then, I just want to get name result :

LKJLKJ_OKJKJ_1900-09-12

So I tried 2 things :

print rbody["name"]
print rbody[1]

But it doesn't work. Have you an idea about command I've to write ?

Thank you

like image 726
Essex Avatar asked Sep 04 '26 09:09

Essex


1 Answers

Given your rbody result it would be

rbody[0]['name']

However if rbody is a string, you need to first turn it into a Python object

>>> import json
>>> s = '''[{"id":8978437,"name":"LKJLKJ_OKJKJ_1900-09-12","parentId":8552450,"description":"","lastModified":"2017-02-06 14:45:40 +0100","type":0,"templateId":null,"templateLocked":0,"creation":"2017-02-06 14:45:40 +0100","creator":"Etat Civil","position":1,"hidden":0,"foldRef":null,"attributes":[],"storage":null,"tags":[]}]'''
>>> json.loads(s)[0]['name']
'LKJLKJ_OKJKJ_1900-09-12'

If you have several dictionaries in your list, you can use a list comprehension

names = [i['name'] for i in json.loads(rbody)]
like image 95
Cory Kramer Avatar answered Sep 07 '26 00:09

Cory Kramer