Valentin Valentin - 4 years ago 100
Python Question

API Rest Python

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

Answer Source

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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download