3

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

3 Answers 3

3

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)]
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. [0] corresponds to what exactly ?
I get this error : Exception Value: string indices must be integers, not str
@Valentin You have a list with a single dict inside. [0] indexes the first element of the list, then ['name'] indexes the dict element with that key.
In your printed json, that appears within a list, that's why you need to index that before accessing the name key of the dictionary.
Note that with requests you can just use response.json instead of json.loads(response.content).
|
2

The return value of your code is a list with one dictionary. It will work like this:

print rbody[0]["name"]

This error message means, that the value of rbody is of a string type. If you want to access the dictionary object, you have to decode the string type value like this:

import json
rbody = json.loads(rbody)

Enjoy!

1 Comment

It doesn't seem to work : Exception Value: string indices must be integers, not str
-1

print rbody[0][1] I hope this help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.