{
"incidents": [
{
"id": "48348840",
"type": 3,
"severity": 2,
"eventCode": 214,
"lat": 39.742012,
"lng": -105.014618,
"startTime": "2017-06-30T10:05:00",
"endTime": "2017-06-30T12:19:38",
"impacting": true,
"shortDesc": "I-25 N/B: delays of four minutes between Exits 207B,208 and Exits 210A,210B",
"fullDesc": "Delays of four minutes on I-25 Northbound between Exits 207B,208 US-85 Santa Fe Dr and Exits 210A,210B I-70 Bus Colfax Ave.",
"delayFromFreeFlow": 3.9700000286102295,
"delayFromTypical": 1.5199999809265137,
"distance": 4.849999904632568,
"iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
"parameterizedDescription": {
"crossRoad2": "I-25 Exits 210A,210B / I-70 Bus Colfax Ave / Auraria Pkwy",
"crossRoad1": "I-25 Exits 207B,208 / US-85 Santa Fe Dr / Kalamath St",
"position2": "and",
"direction": "Northbound",
"position1": "between",
"eventText": "XDFI Delays",
"roadName": "I-25"
}
},
{
"id": "48356745",
"type": 3,
"severity": 2,
"eventCode": 214,
"lat": 39.751297,
"lng": -105.016808,
"startTime": "2017-06-30T11:24:00",
"endTime": "2017-06-30T12:15:54",
"impacting": true,
"shortDesc": "I-25 S/B: delays of three minutes between Exit 213 I-70 and Exit 211 23rd Ave",
"fullDesc": "Delays of three minutes on I-25 Southbound between Exit 213 I-70 and Exit 211 23rd Ave.",
"delayFromFreeFlow": 2.680000066757202,
"delayFromTypical": 1.9299999475479126,
"distance": 3.509999990463257,
"iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
"parameterizedDescription": {
"crossRoad2": "I-25 Exit 211 / 23rd Ave / Water St",
"crossRoad1": "I-70 Exit 274 / I-25 Exit 213 / US-6",
"position2": "and",
"direction": "Southbound",
"position1": "between",
"eventText": "XDFI Delays",
"roadName": "I-25"
}
},
{
"id": "48347260",
"type": 3,
"severity": 2,
"eventCode": 74,
"lat": 39.780136,
"lng": -104.966866,
"startTime": "2017-06-30T09:34:00",
"endTime": "2017-06-30T12:20:39",
"impacting": true,
"shortDesc": "I-70 W/B: delays increasing between Exit 278 and Exit 275B",
"fullDesc": "Delays increasing and delays of three minutes on I-70 Westbound between Exit 278 CO-35 Quebec St and Exit 275B CO-265 Brighton Blvd. Average speed 25 mph.",
"delayFromFreeFlow": 3.9700000286102295,
"delayFromTypical": 0,
"distance": 5.429999828338623,
"iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
"parameterizedDescription": {
"crossRoad2": "I-70 Exit 275B / CO-265 Brighton Blvd / Brighton Blvd",
"crossRoad1": "I-70 Exit 278 / CO-35 Quebec St / CO-35 Northfield Quebec St",
"position2": "and",
"direction": "Westbound",
"position1": "between",
"eventText": "XDFI Delays increasing, xDFI Delays, xDFI Average Speed",
"roadName": "I-70"
}
},
{
"id": "48347262",
"type": 3,
"severity": 1,
"eventCode": 73,
"lat": 39.804871,
"lng": -104.939171,
"startTime": "2017-06-30T09:28:00",
"endTime": "2017-06-30T12:18:46",
"impacting": true,
"shortDesc": "I-270 W/B: delays of eight minutes in Commerce City",
"fullDesc": "Delays of eight minutes and delays easing on I-270 Westbound in Commerce City. Average speed 15 mph.",
"delayFromFreeFlow": 8.350000381469727,
"delayFromTypical": 6.380000114440918,
"distance": 5,
"iconURL": "http://content.mqcdn.com/mqtraffic/congestion_min.png",
"parameterizedDescription": {
"crossRoad2": "I-270",
"crossRoad1": "I-70 Exits 279A,279B / I-270 Exit 5 / Central Park Blvd",
"position2": "and",
"direction": "Westbound",
"position1": "between",
"eventText": "XDFI Delays, xDFI Delays easing, xDFI Average Speed",
"roadName": "I-270"
}
}
],
"mqUrl": "http://www.mapquest.com/maps?traffic=1&latitude=39.73831084129981&longitude=-104.9850082397461",
"info": {
"copyright": {
"text": "© 2017 MapQuest, Inc.",
"imageUrl": "https://api-s.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2017 MapQuest, Inc."
},
"messages": [],
"statuscode": 0
}
}
Hi in the above code I want to search a particular node in the above xml tree say I am interested in the node roadName :I-25 and display the road name so can anyone guide me how this can be done ? finding a single node / subnode of interest in the xml tree with the help of element tree ?
-
This is JSON though.cs95– cs952017年06月30日 18:04:28 +00:00Commented Jun 30, 2017 at 18:04
1 Answer 1
As mentioned in the comment, what you have is JSON data (see related: Python - Parsing JSON Data Set).
Use the json module:
import json
# if data is a json string
json_data = json.loads(data) # or use json.load(data) if data is a file
To get the values you want, index the respective keys from the data. In this case the parent node is 'incidents', of which you want the second list item (zero-indexed, so use [1]), then dig down through the corresponding keys:
print json_data['incidents'][1]['parameterizedDescription']['roadName']
will output:
I-25