-2

I'm about 2 days into Python (2.7 I think) and I'm trying to make the API response here a lot more readable, at the moment it's hard to decipher the English response.

import json, requests

response = requests.get("http://data.dublinked.ie/cgi-
bin/rtpi/realtimebusinformation?stopid=1341&routeid=49&format=json")

data = response.json()

print data

The response is this, and it's very ugly :( I would like to A) format it so it's more readable, maybe in a list or key:value pair? B) How do pick only certain information from the response?

numberofresults: 2
timestamp: 16/01/2018 17:04:15
errormessage: 
results: [{u'origin': u'Tallaght', u'direction': u'Inbound',
u'destinationlocalized': u'Sr\xe1id an Phiarsaigh', u'monitored': u'true',         
u'departureduetime': u'6', u'route': u'49', u'additionalinformation': u'',         
u'destination': u'Pearse St', u'scheduleddeparturedatetime': u'16/01/2018 
17:09:00', u'scheduledarrivaldatetime': u'16/01/2018 17:09:00', 
u'sourcetimestamp': u'16/01/2018 17:01:53', u'operator': u'bac', 
u'departuredatetime': u'16/01/2018 17:10:56', u'arrivaldatetime': 
u'16/01/2018 17:10:56', u'lowfloorstatus': u'no', u'originlocalized':                                 
u'Tamhlacht', u'duetime': u'6'}, {u'origin': u'Tallaght', u'direction': 
u'Inbound', u'destinationlocalized': u'Sr\xe1id an Phiarsaigh', 
u'monitored': u'true', u'departureduetime': u'37', u'route': u'49', 
u'additionalinformation': u'', u'destination': u'Pearse St', 
u'scheduleddeparturedatetime': u'16/01/2018 17:42:00', 
u'scheduledarrivaldatetime': u'16/01/2018 17:42:00', u'sourcetimestamp': 
u'16/01/2018 16:43:44', u'operator': u'bac', u'departuredatetime': 
u'16/01/2018 17:42:13', u'arrivaldatetime': u'16/01/2018 17:42:13', 
u'lowfloorstatus': u'no', u'originlocalized': u'Tamhlacht', u'duetime': 
u'37'}]
errorcode: 0
stopid: 1341
[Finished in 0.7s]

I'm most probably doubling up on a question/answer but I can't seem to find it! Sorry. Also, the "u" is something I don't understand either.

Thank you all in advance x

bfresh
  • 150
  • 3
  • 14
  • Why are you using python2.7? Will not be maintained [past 2020](https://pythonclock.org/). – Juan Antonio Jan 16 '18 at 17:20
  • The response is already in key:value pairs. You have a list containing a dictionary. Try `print(results[0]['destination'])` for example. The `u` is a superficial tag displayed by python to say that the strings are Unicode. – roganjosh Jan 16 '18 at 17:23
  • The 'u' stands for unicode string.And like rohan said its probably in a key-value pair – Rakesh Jan 16 '18 at 17:32
  • See [this](https://stackoverflow.com/q/2464959/4799172) re: u prefix. As for formatting, it's hard to say what you want. The response is a flat dictionary so pretty printing will probably just give you a list so you'll need to search for python string formatting if you want the printed output in some more elaborate format. – roganjosh Jan 16 '18 at 17:36

2 Answers2

2

The json library in python is very useful especially in working with JSON objects. I would specifically look at the json.dump and json.dumps methods. If you look at the official documentation, json.dumps returns a string and takes a separators parameter and indent parameter.

import json, requests

response = requests.get("http://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=1341&routeid=49&format=json")
print(json.dumps(response.json(), separators=(",",":"), indent=4)

As noted above, the u before a string denotes a unicode string, which you can safely overlook for now.

Jake Raper
  • 66
  • 5
1

The 'u' stands for unicode string. The return type is a key-value(dictionary) datatype

import json, requests
import pprint

response = requests.get("http://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=1341&routeid=49&format=json")
data = response.json()

pprint.pprint(data)  #Better view
print data['numberofresults']  #Key-Value
Rakesh
  • 81,458
  • 17
  • 76
  • 113