-1

how can i output the title alphabetically? i want it to print 'entertainment' then 'information'.

note: this is just a sample. the json file i am working with has many more titles

>>> import json

>>> field = json.loads('{"js":[{"id":"1","title":"information","number":"1","alias":"information"},{"id":"2","title":"entertainments","number":"2","alias":"entertainments"}],"text":""}')

>>> for data in field['js']:
...     print data['title']
... 

information
entertainments
cann0nextr3me
  • 129
  • 1
  • 1
  • 7

2 Answers2

0

Sort the list using a key:

field['js'].sort(key=lambda k: k['title'])

For more info, see How do I sort a list of dictionaries by values of the dictionary in Python?

Community
  • 1
  • 1
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
0

One way:

>>> for data in sorted(field['js']):
...   print data['title']
... 
entertainments
information
flyingfinger
  • 690
  • 12
  • 16