0

I have a TinyDB for Python and the data with multiple lines in the structure {'service': {'key1': '', 'key2': '', 'key3': '', 'key4': '', 'key5': ''}}

I wanted to get only "service" from all the lines of the database assuming the "service" real name is not known.

How to achieve with tinyDB Query()?

db.all()[0] just give entire line in index 0.

I am expecting to get only "service".

Venu Reddy
  • 39
  • 8

2 Answers2

2

It's a bit tricky:

>>> list(db.all()[0].keys())[0]
service

>>> list(db.all()[0].values())
[{'key1': '', 'key2': '', 'key3': '', 'key4': '', 'key5': ''}]
Alderven
  • 7,569
  • 5
  • 26
  • 38
0

To get service from all the outputs,

def get_key_info(db_output, key):
   return [row[key] for row in db_output]

output = db.all()
get_key_info(output, 'service')

To just get the service first row,

db.get(doc_id=1)['service']
shaik moeed
  • 5,300
  • 1
  • 18
  • 54