Skip to content
Advertisement

How to get key values in a json object(Python)

This is my json :

{'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}

I want to get a list of all the values of name.
What should be my code in python

Advertisement

Answer

d.values gives all the values, then you can get the attribute name of each value.

d = {'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}

[i['name'] for i in d.values()]
['poulami', 'test']

Also note that d.values returns a generator and not a list so to convert to list use list(d.values())

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement