Python Dict prints last place on first place [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I made the following dict:
logs = {
'3h forecast': {
'Ort': '',
'Datum und Uhrzeit': '',
'Temperatur': '',
'Temperatur_Min': '',
'Temperatur_Max': ''},
'6h forecast': {
'Ort': '',
'Datum und Uhrzeit': '',
'Temperatur': '',
'Temperatur_Min': '',
'Temperatur_Max': ''},
'9h forecast': {
'Ort': '',
'Datum und Uhrzeit': '',
'Temperatur': '',
'Temperatur_Min': '',
'Temperatur_Max': ''},
'12h forecast': {
'Ort': '',
'Datum und Uhrzeit': '',
'Temperatur': '',
'Temperatur_Min': '',
'Temperatur_Max': ''}
}
and print it with following line:
print(json.dumps(logs, sort_keys=True, indent=4))
and the result in the console is this:
{
"12h forecast": {
"Datum und Uhrzeit": "",
"Ort": "",
"Temperatur": "",
"Temperatur_Max": "",
"Temperatur_Min": ""
},
"3h forecast": {
"Datum und Uhrzeit": "",
"Ort": "",
"Temperatur": "",
"Temperatur_Max": "",
"Temperatur_Min": ""
},
"6h forecast": {
"Datum und Uhrzeit": "",
"Ort": "",
"Temperatur": "",
"Temperatur_Max": "",
"Temperatur_Min": ""
},
"9h forecast": {
"Datum und Uhrzeit": "",
"Ort": "",
"Temperatur": "",
"Temperatur_Max": "",
"Temperatur_Min": ""
}
}
So... Why is the 12h forecast now on the first place and not like the logs dict on the last place?
What am I doing wrong? :)

just remove sort_keys=True
print(json.dumps(logs, indent=4))
#output
{
"3h forecast": {
"Ort": "",
"Datum und Uhrzeit": "",
"Temperatur": "",
"Temperatur_Min": "",
"Temperatur_Max": ""
},
"6h forecast": {
"Ort": "",
"Datum und Uhrzeit": "",
"Temperatur": "",
"Temperatur_Min": "",
"Temperatur_Max": ""
},
"9h forecast": {
"Ort": "",
"Datum und Uhrzeit": "",
"Temperatur": "",
"Temperatur_Min": "",
"Temperatur_Max": ""
},
"12h forecast": {
"Ort": "",
"Datum und Uhrzeit": "",
"Temperatur": "",
"Temperatur_Min": "",
"Temperatur_Max": ""
}
}

What happened here is that you were sorting the keys based on strings.
So the '1' from '12' got compared with the '3' from the other forecast.
And because '1' is lower than '3' in the ASCII table it is placed earlier in the list.
You can check this behavior yourself by comparing
'9' < '10'
to
9 < 10
To prevent this is in this specific case you can remove the sort_keys attribute.

Related

How can I change the format of a JSON output from iterating through a python dictionary

i'm trying to change the format of a json file from:
{
"ordnungsrufe": [
{
"date": "1961-04-18",
"president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
"wp": "3",
"protocol": "Protokoll der 154. Sitzung des 3. Deutschen Bundestages",
"source": "https://dserver.bundestag.de/btp/03/03154.pdf",
"calledOutName": "Adolf Ludwig, MdB",
"calledOutParty": "SPD"
}
]
}
to:
[
{
"calledOut": {
"name": "Adolf Ludwig, MdB",
"party": "SPD"
},
"date": "1961-04-18",
"president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
"source": {
"pdf": "https://dserver.bundestag.de/btp/03/03154.pdf",
"sectionFrom": "",
"sectionTo": "",
"linkToVideo": ""
}
}
]
I'have build this python script, but I'm struggling to get the expected format.
import json
import codecs
data = json.load(codecs.open('data.json', 'r', 'utf-8-sig'))
#print(data)
ordnungsruf = {}
for person in data['ordnungsrufe']:
ordnungsruf[person['calledOutName']] = {
"calledOut" : {
"name": person["calledOutName"],
"party": person["calledOutParty"]
},
"date": person["date"],
"president" : person["president"],
"source" : {
"pdf": person["source"],
"sectionFrom": "",
"sectionTo": "",
"linkToVideo": "",
}
}
with open('ordnungsrufe_ordered.json', 'w') as json_file:
json.dump(ordnungsruf, json_file, indent=2)
my output:
{
"Adolf Ludwig, MdB": {
"calledOut": {
"name": "Adolf Ludwig, MdB",
"party": "SPD"
},
"date": "1954-12-08",
"president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
"source": {
"pdf": "https://dserver.bundestag.de/btp/02/02059.pdf",
"sectionFrom": "",
"sectionTo": "",
"linkToVideo": ""
}
},
}
I tried to iterate through the dictionary with several different methods and I'm always failing to get the right JSON. This example seems to be the closest.
Is there a better way to iterate through a dictionary to change the format of a python script?
Putting results into a list should work:
result = []
for item in data['ordnungsrufe']:
result.append({
'calledOut': {
'name': item['calledOutName'],
'party': item['calledOutParty']
},
'date': item['date'],
'president': item['president'],
'source': {
'pdf': item['source'],
'sectionFrom': '',
'sectionTo': '',
'linkToVideo': ''
}
})
print(result)
N.b. it might be a good idea to save the result as a dictionary:
with open('target.json', 'w') as f:
json.dump({'items': result}, f)
as it will have been a bit more future proof.

Add/Remove JSON sub-tree in Python

Here is the empty JSON that I intend to use in my back end :
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
Each user might have more than one Merchant ID. As such, I would need to be able to add another whole merchant_id subtree as followed :
{
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
"merchant_id2": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
Is there any easy way to achieve the addition/removal of a JSON in Python?
Thanks a lot !
You can try .update()
base = {
"user": "",
"mids": {
"merchant_id": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
another_merchant = {"merchant_id2": {
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
base["mids"].update(another_merchant)
print(base)
{
"user": "",
"mids":
{
"merchant_id":
{
"name": "",
"cruise_credentials":
{
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB":
{
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
"merchant_id2":
{
"name": "",
"cruise_credentials":
{
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB":
{
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
}
}
Thanks a lot for your help.
There is 2 solutions depending on how we would handle this.
Using a LIST of merchant_id would allow for simple addition of a new dictionary NAME after the first NAME dictionary.
{
"user": "",
"merchant_id": [
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
},
{
"name": "",
"cruise_credentials": {
"APIkey": "",
"APIidentifier": "",
"OrgUnitId": ""
},
"SAWB": {
"ProfileID": "",
"AccesKey": "",
"SecretKey": ""
}
}
]
}
Or we can use the .update() function. However, this would force us to change the field "merchant_name" ourself to merchant _name2 and would require additional logic.

How to map fields in NumPy

I have a script that writes a JSON web-service to an esri file geodatabase. There is a 1-M relationship between address and requests, the requests are read and written as 3 fields. Below is an example of JSON response and how the data appears in a table. I would like my data to read as one feature/row with multiple request types and quantities associated with the one address and Service Request number. i.e. Address, Type, E-Waste Item 1, Quantity 1, E-Waste Item 2, Quantity 2, etc..
{
"Response": {
"ListOfServiceRequest": {
"ServiceRequest": [
{
"ActionTaken": "",
"AddressVerified": "Y",
"Anonymous": "N",
"AssignTo": "EV",
"Assignee": "",
"CreatedByUserLogin": "MYLATHREEONEONE",
"CreatedDate": "02/17/2015 16:53:25",
"CustomerAccessNumber": "",
"Email": "mylathreeoneone#gmail.com",
"FirstName": "Myla",
"HomePhone": "2131234567",
"IntegrationId": "02172015165417667",
"LADWPAccountNo": "",
"Language": "",
"LastName": "Threeoneone",
"Latitude": "34.176277",
"ListOfAuditTrailItem2": {
"AuditTrailItem2": [
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Status",
"NewValue": "Closed",
"OldValue": "Open"
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Updated By User",
"NewValue": "EAIBOS",
"OldValue": "MYLATHREEONEONE"
}
]
},
"ListOfChildServiceRequest": {},
"ListOfLa311BarricadeRemoval": {},
"ListOfLa311BeesOrBeehive": {},
"ListOfLa311BillingCsscAdjustment": {},
"ListOfLa311BillingEccAdjustment": {},
"ListOfLa311BillingRsscAdjustment": {},
"ListOfLa311BillingRsscExemption": {},
"ListOfLa311BrushItemsPickup": {},
"ListOfLa311BulkyItem": {},
"ListOfLa311BusPadLanding": {},
"ListOfLa311Containers": {},
"ListOfLa311CurbRepair": {},
"ListOfLa311DeadAnimalRemoval": {},
"ListOfLa311DocumentLog": {},
"ListOfLa311ElectronicWaste": {
"La311ElectronicWaste": [
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "Moody",
"DriverLastName": "Frederick10/09/2014",
"ElectronicWestType": "Microwaves",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegalDumpCollectionLoc": "",
"IllegallyDumped": "N",
"ItemCount": "3",
"LastUpdatedBy": "",
"MobileHomeSpace": "",
"Name": "021720151654176711",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "SC Truck 10",
"Type": "Electronic Waste"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "Moody",
"DriverLastName": "Frederick10/09/2014",
"ElectronicWestType": "Televisions (Any Size)",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegalDumpCollectionLoc": "",
"IllegallyDumped": "N",
"ItemCount": "6",
"LastUpdatedBy": "",
"MobileHomeSpace": "",
"Name": "021720151654176722",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "SC Truck 10SC Truck 10",
"Type": "Electronic Waste"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "Moody",
"DriverLastName": "Frederick10/09/2014",
"ElectronicWestType": "VCR/DVD Players",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegalDumpCollectionLoc": "",
"IllegallyDumped": "N",
"ItemCount": "1",
"LastUpdatedBy": "",
"MobileHomeSpace": "",
"Name": "021720151654176723",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "SC Truck 10SC Truck 10",
"Type": "Electronic Waste"
}
]
},
"ListOfLa311Flooding": {},
"ListOfLa311GeneralStreetInspection": {},
"ListOfLa311GenericBc": {
"La311GenericBc": [
{
"ATTRIB_08": "N",
"ATTRIB_16": "",
"ListOfLa311GenericbcAuditTrail": {
"La311GenericbcAuditTrail": [
{
"Date": "02/27/2015 15:15:28",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick10/09/2014",
"OldValue": "Frederick"
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver First Name",
"NewValue": "Moody",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Truck No",
"NewValue": "SC Truck 10",
"OldValue": ""
}
]
},
"NAME": "021720151654176711",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHR",
"TYPE": "Electronic Waste"
},
{
"ATTRIB_08": "N",
"ATTRIB_16": "",
"ListOfLa311GenericbcAuditTrail": {
"La311GenericbcAuditTrail": [
{
"Date": "02/27/2015 15:15:28",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick10/09/2014",
"OldValue": "Frederick"
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver First Name",
"NewValue": "Moody",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Truck No",
"NewValue": "SC Truck 10SC Truck 10",
"OldValue": ""
}
]
},
"NAME": "021720151654176722",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHS",
"TYPE": "Electronic Waste"
},
{
"ATTRIB_08": "N",
"ATTRIB_16": "",
"ListOfLa311GenericbcAuditTrail": {
"La311GenericbcAuditTrail": [
{
"Date": "02/27/2015 15:15:28",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick10/09/2014",
"OldValue": "Frederick"
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver First Name",
"NewValue": "Moody",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Driver Last Name",
"NewValue": "Frederick",
"OldValue": ""
},
{
"Date": "02/27/2015 11:45:46",
"EmployeeLogin": "EAIBOS",
"Field": "Truck No",
"NewValue": "SC Truck 10SC Truck 10",
"OldValue": ""
}
]
},
"NAME": "021720151654176723",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHT",
"TYPE": "Electronic Waste"
},
{
"ATTRIB_08": "",
"ATTRIB_16": "",
"ListOfLa311GenericbcAuditTrail": {},
"NAME": "02172015165417667100",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHU",
"TYPE": "GIS"
},
{
"ATTRIB_08": "",
"ATTRIB_16": "",
"ListOfLa311GenericbcAuditTrail": {},
"NAME": "021720151654176671",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHQ",
"TYPE": "SR Photo ID"
}
]
},
"ListOfLa311GisLayer": {
"La311GisLayer": [
{
"A_Call_No": "",
"Area": "0",
"BOSRadioHolderName": "",
"CommunityPlanningArea": "",
"Day": "THURSDAY",
"DirectionSuffix": "",
"DistrictAbbr": "",
"DistrictName": "EV",
"DistrictNumber": "",
"DistrictOffice": "",
"Fraction": "",
"LastUpdatedBy": "",
"Name": "02172015165417667100",
"R_Call_No": "",
"SectionId": "",
"ShortDay": "Thu",
"StreetFrom": "",
"StreetLightId": "",
"StreetLightStatus": "",
"StreetTo": "",
"Type": "GIS",
"Y_Call_No": ""
}
]
},
"ListOfLa311GraffitiRemoval": {},
"ListOfLa311GuardWarningRailMaintenance": {},
"ListOfLa311GutterRepair": {},
"ListOfLa311HomelessEncampment": {},
"ListOfLa311IllegalAutoRepair": {},
"ListOfLa311IllegalConstruction": {},
"ListOfLa311IllegalConstructionFence": {},
"ListOfLa311IllegalDischargeOfWater": {},
"ListOfLa311IllegalDumpingInProgress": {},
"ListOfLa311IllegalDumpingPickup": {},
"ListOfLa311IllegalExcavation": {},
"ListOfLa311IllegalSignRemoval": {},
"ListOfLa311IllegalVending": {},
"ListOfLa311InformationOnly": {},
"ListOfLa311LandMudSlide": {},
"ListOfLa311LeafBlowerViolation": {},
"ListOfLa311ManualPickup": {},
"ListOfLa311MedianIslandMaintenance": {},
"ListOfLa311MetalHouseholdAppliancesPickup": {},
"ListOfLa311MoveInMoveOut": {},
"ListOfLa311MultipleStreetlightIssue": {},
"ListOfLa311NewsRackViolation": {},
"ListOfLa311Obstructions": {},
"ListOfLa311Other": {},
"ListOfLa311OvergrownVegetationPlants": {},
"ListOfLa311PalmFrondsDown": {},
"ListOfLa311Pothole": {},
"ListOfLa311Resurfacing": {},
"ListOfLa311SanitationBillingBif": {},
"ListOfLa311SanitationBillingCssc": {},
"ListOfLa311SanitationBillingEcc": {},
"ListOfLa311SanitationBillingInquiry": {},
"ListOfLa311SanitationBillingLifeline": {},
"ListOfLa311SanitationBillingRssc": {},
"ListOfLa311SanitationBillingSrf": {},
"ListOfLa311ServiceNotComplete": {},
"ListOfLa311ServiceRequestNotes": {
"La311ServiceRequestNotes": [
{
"Comment": "Out on the sidewalk near the curb. Hopefully it is still there.",
"CommentType": "Address Comments",
"CreatedByUser": "MYLATHREEONEONE",
"CreatedDate": "02/17/2015 16:53:26",
"Date1": "",
"Date2": "",
"Date3": "",
"FeedbackSRType": "",
"IntegrationId": "021720151654176661",
"IsSrNoAvailable": "",
"ListOfLa311SrNotesAuditTrail": {},
"Notification": "N",
"Text1": ""
},
{
"Comment": "So glad to get rid of this old junk. Thanks.",
"CommentType": "External",
"CreatedByUser": "MYLATHREEONEONE",
"CreatedDate": "02/17/2015 16:53:26",
"Date1": "",
"Date2": "",
"Date3": "",
"FeedbackSRType": "",
"IntegrationId": "021720151654176662",
"IsSrNoAvailable": "",
"ListOfLa311SrNotesAuditTrail": {},
"Notification": "N",
"Text1": ""
}
]
},
"ListOfLa311SidewalkRepair": {},
"ListOfLa311SingleStreetlightIssue": {},
"ListOfLa311SrPhotoId": {
"La311SrPhotoId": [
{
"LastUpdatedBy": "",
"Name": "021720151654176671",
"PhotoId": "https://myla311.lacity.org/portal/docview?id=04b8ba678fe21d32b05673eb9ad7711b",
"Type": "SR Photo ID"
}
]
},
"ListOfLa311StreetSweeping": {},
"ListOfLa311StreetTreeInspection": {},
"ListOfLa311StreetTreeViolations": {},
"ListOfLa311SubscribeDuplicateSr": {},
"ListOfLa311TablesAndChairsObstructing": {},
"ListOfLa311TreeEmergency": {},
"ListOfLa311TreeObstruction": {},
"ListOfLa311TreePermits": {},
"ListOfLa311WeedAbatementForPrivateParcels": {},
"LoginUser": "",
"Longitude": "-118.455249",
"MobilOS": "",
"NewContactEmail": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"Owner": "BOS",
"ParentSRLinkDate": "",
"ParentSRLinkUser": "",
"ParentSRNumber": "",
"ParentSRStatus": "",
"ParentSRType": "",
"Priority": "Normal",
"ReasonCode": "",
"RescheduleCounter": "",
"ResolutionCode": "SW",
"SRAddress": "5810 N WILLIS AVE, 91411",
"SRAddressName": "",
"SRAreaPlanningCommission": "South Valley APC",
"SRAreaPlanningCommissionId": "3",
"SRCity": "",
"SRCommunityPoliceStation": "VALLEY BUREAU",
"SRCommunityPoliceStationAPREC": "VAN NUYS",
"SRCommunityPoliceStationPREC": "9",
"SRCouncilDistrictMember": "Tom LaBonge",
"SRCouncilDistrictNo": "4",
"SRCrossStreet": "",
"SRDirection": "N",
"SRHouseNumber": "5810",
"SRNeighborhoodCouncilId": "20",
"SRNeighborhoodCouncilName": "VAN NUYS NC",
"SRNumber": "1-3580171",
"SRStreetName": "WILLIS",
"SRSuffix": "AVE",
"SRTBColumn": "J",
"SRTBMapGridPage": "561",
"SRTBRow": "1",
"SRType": "Electronic Waste",
"SRUnitNumber": "5810",
"SRXCoordinate": "6423983",
"SRYCoordinate": "1886848",
"ServiceDate": "02/19/2015 00:00:00",
"Source": "",
"Status": "Closed",
"UpdatedByUserLogin": "EAIBOS",
"UpdatedDate": "02/27/2015 15:14:22",
"Zipcode": "91411"
}
]
},
"NumOutputObjects": "1"
Script
import json
import jsonpickle
import requests
import arcpy
fc = "C:\MYLATesting.gdb\MYLA311"
if arcpy.Exists(fc):
arcpy.Delete_management(fc)
ListTable ="C:\MYLATesting.gdb\ListView"
if arcpy.Exists(ListTable):
arcpy.Delete_management(ListTable)
f2 = open('C:\Users\Administrator\Desktop\DetailView.json', 'r')
data2 = jsonpickle.encode( jsonpickle.decode(f2.read()) )
url2 = "myURL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}
r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)
items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
SRAddress = sr['SRAddress']
latitude = sr['Latitude']
longitude = sr['Longitude']
for ew in sr["ListOfLa311ElectronicWaste"][u"La311ElectronicWaste"]:
CommodityType = ew['Type']
ItemType = ew['ElectronicWestType']
ItemCount = ew['ItemCount']
items.append((SRAddress,
latitude,
longitude,
CommodityType,
ItemType,
ItemCount))
import numpy as np #NOTE THIS
dt = np.dtype([('SRAddress', 'U40'),
('latitude', '<f8'),
('longitude', '<f8'),
('Type', 'U40'),
('ElectronicWestType', 'U40'),
('ItemCount', 'U40')])
arr = np.array(items,dtype=dt)
sr = arcpy.SpatialReference(4326)
arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitude', 'latitude'], sr )
print json.dumps(decoded2, sort_keys=True, indent=4)

Handling multiple JSON data sets for POST request in Python

I have 3 JSON methods which are two query methods and an update method. I would like to parse through this information with and execute a POST request and pass this data into a database using arcpy for GIS use for all three methods. I have a script which works and gets a response, however the problem is locating the keys and values for each object so that I can successfully send my values to to a database.
Additionally, I handle this task with three different methods, all of which I need data from.
For instance,
query 1 would allow me to parse and find the address, lat/lng, etc.
query 2 would allow me to parse and find customer info, type of request, etc.
query 3 would allow me to update a request.
My first question is how do I successfully extract only the data that I want from each output; I have tested Key/Values in POSTman to no avail, the server is expecting an entire JSON file.
My second question is how do I handle 3 different requests; I am assuming 3 different post methods and selecting the data that I want.
Example of JSON one query passed to server
{
"RequestSpecificDetail": {
"ParentSRNumberForLink": ""
},
"MetaData": {
"appVersion": "1.34",
"deviceModel": "x86_64",
"dateAndTime": "01/15/2015 12:46:36",
"deviceToken": "A2C1DD9D-D17D-4031-BA3E-977C250BFD58",
"osVersion": "8.1"
},
"SRData": {
"LoginUser": "User89",
"NewContactEmail": "abc#gmail.com",
"UpdatedDate": "02/05/2015"
}
}
Example of Query 1 Output
{
"status": {
"code": 311,
"message": "Service Request Successfully Queried.",
"cause": ""
},
"Response": {
"NumOutputObjects": "2",
"ListOfServiceRequest": {
"ServiceRequest": [
{
"SRAddress": "1200 W TEMPLE ST, 90026",
"SRNumber": "1-5099871",
"SRType": "Feedback",
"CreatedDate": "02/05/2015 22:55:58",
"UpdatedDate": "02/05/2015 22:55:58",
"Status": "Open",
"imageURL": ""
},
{
"SRAddress": "1200 W TEMPLE ST, 90026",
"SRNumber": "1-5133051",
"SRType": "Feedback",
"CreatedDate": "02/05/2015 23:03:54",
"UpdatedDate": "02/05/2015 23:03:54",
"Status": "Open",
"imageURL": "https://SERVER_END_POINT/portal/docview?id=fe083ae14b52b1af0945b4d756c296a5"
}
]
},
"LastUpdateDate": "02/05/2015"
}
}
Example of Query 2 passed to server
{
"RequestSpecificDetail": {
"ParentSRNumberForLink": ""
},
"MetaData": {
"appVersion": "1.34",
"deviceModel": "x86_64",
"dateAndTime": "01/15/2015 12:46:36",
"deviceToken": "A2C1DD9D-D17D-4031-BA3E-977C250BFD58",
"osVersion": "8.1"
},
"SRData": {
"SRNumber": "1-1080871"
}
}
Query two output
{
"status": {
"code": 311,
"message": "Service Request Successfully Queried.",
"cause": ""
},
"Response": {
"NumOutputObjects": "1",
"ListOfServiceRequest": {
"ServiceRequest": [
{
"AddressVerified": "Y",
"SRNumber": "1-1080871",
"SRType": "Homeless Encampment",
"CreatedDate": "12/31/2014 13:49:23",
"UpdatedDate": "12/31/2014 13:49:23",
"IntegrationId": "1420033765921",
"Status": "Open",
"CreatedByUserLogin": "User89",
"UpdatedByUserLogin": "User89",
"Anonymous": "N",
"Zipcode": "90026",
"Latitude": "34.064937",
"Longitude": "-118.252968",
"CustomerAccessNumber": "",
"LADWPAccountNo": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"NewContactEmail": "",
"ParentSRNumber": "",
"Priority": "Normal",
"Language": "English",
"ReasonCode": "",
"ServiceDate": "12/31/2014 00:00:00",
"Source": "311",
"Email": "user#email.com",
"FirstName": "User",
"HomePhone": "3123123123",
"LastName": "Pp",
"LoginUser": "",
"ResolutionCode": "",
"SRUnitNumber": "",
"MobilOS": "iOS",
"SRAddress": "1200 W TEMPLE ST, 90026",
"SRAddressName": "",
"SRAreaPlanningCommission": "Central APC",
"SRCommunityPoliceStation": "",
"SRCouncilDistrictMember": "Gilbert Cedillo",
"SRCouncilDistrictNo": "1",
"SRDirection": "W",
"SRNeighborhoodCouncilId": "44",
"SRNeighborhoodCouncilName": "GREATER ECHO PARK ELYSIAN NC",
"SRStreetName": "TEMPLE",
"SRSuffix": "ST",
"SRTBColumn": "E",
"SRTBMapGridPage": "634",
"SRTBRow": "2",
"SRXCoordinate": "6485064",
"SRYCoordinate": "1846114",
"AssignTo": "North Central - 104 - IED",
"Assignee": "Siebel Administrator",
"Owner": "BSS",
"ParentSRStatus": "",
"ParentSRType": "",
"ParentSRLinkDate": "",
"ParentSRLinkUser": "",
"SRAreaPlanningCommissionId": "4",
"SRCommunityPoliceStationAPREC": "RAMPART",
"SRCommunityPoliceStationPREC": "2",
"SRCrossStreet": "",
"ActionTaken": "",
"SRCity": "",
"RescheduleCounter": "",
"SRHouseNumber": "",
"ListOfDataBarricadeRemoval": {},
"ListOfDataBulkyItem": {},
"ListOfDataDeadAnimalRemoval": {},
"ListOfDataGraffitiRemoval": {},
"ListOfDataInformationOnly": {},
"ListOfDataMultipleStreetlightIssue": {},
"ListOfDataSingleStreetlightIssue": {},
"ListOfDataSrPhotoId": {
"DataSrPhotoId": []
},
"ListOfDataBusPadLanding": {},
"ListOfDataCurbRepair": {},
"ListOfDataFlooding": {},
"ListOfDataGeneralStreetInspection": {},
"ListOfDataGuardWarningRailMaintenance": {},
"ListOfDataGutterRepair": {},
"ListOfDataLandMudSlide": {},
"ListOfDataPothole": {},
"ListOfDataResurfacing": {},
"ListOfDataSidewalkRepair": {},
"ListOfDataStreetSweeping": {},
"ListOfDataBeesOrBeehive": {},
"ListOfDataMedianIslandMaintenance": {},
"ListOfDataOvergrownVegetationPlants": {},
"ListOfDataPalmFrondsDown": {},
"ListOfDataStreetTreeInspection": {},
"ListOfDataStreetTreeViolations": {},
"ListOfDataTreeEmergency": {},
"ListOfDataTreeObstruction": {},
"ListOfDataTreePermits": {},
"ListOfDataBrushItemsPickup": {},
"ListOfDataContainers": {},
"ListOfDataElectronicWaste": {},
"ListOfDataIllegalDumpingPickup": {},
"ListOfDataManualPickup": {},
"ListOfDataMetalHouseholdAppliancesPickup": {},
"ListOfDataMoveInMoveOut": {},
"ListOfDataHomelessEncampment": {
"DataHomelessEncampment": [
{
"ApprovedBy": "",
"AssignedTo": "",
"CompletedBy": "",
"Contact": "",
"ContactDate": "",
"Crew": "",
"DateCompleted": "12/31/2014 00:00:00",
"InspectedBy": "",
"InspectionDate": "",
"Location": "Alley",
"Type": "Homeless Encampment",
"LastUpdatedBy": "",
"OptionalTrackingCode": "",
"Name": "a5b5b2b9-d2e7-400a-bf75-1138ff013caa"
}
]
},
"ListOfDataIllegalAutoRepair": {},
"ListOfDataIllegalConstruction": {},
"ListOfDataIllegalConstructionFence": {},
"ListOfDataIllegalDischargeOfWater": {},
"ListOfDataIllegalDumpingInProgress": {},
"ListOfDataIllegalExcavation": {},
"ListOfDataIllegalSignRemoval": {},
"ListOfDataIllegalVending": {},
"ListOfDataLeafBlowerViolation": {},
"ListOfDataNewsRackViolation": {},
"ListOfDataObstructions": {},
"ListOfDataTablesAndChairsObstructing": {},
"ListOfDataGisLayer": {
"DataGisLayer": [
{
"A_Call_No": "",
"Area": "",
"Day": "",
"DirectionSuffix": "",
"DistrictAbbr": "",
"DistrictName": "Central",
"DistrictNumber": "104",
"DistrictOffice": "North Central",
"Fraction": "",
"R_Call_No": "",
"SectionId": "5279800",
"ShortDay": "",
"StreetFrom": "BOYLSTON ST",
"StreetTo": "FIRMIN ST",
"StreetLightId": "",
"StreetLightStatus": "",
"Type": "GIS",
"Y_Call_No": "",
"Name": "41572025-3803-49c4-8561-6e7ef41775df",
"CommunityPlanningArea": "Westlake",
"LastUpdatedBy": "",
"BOSRadioHolderName": ""
}
]
},
"ListOfDataServiceRequestNotes": {
"DataServiceRequestNotes": [
{
"CreatedDate": "12/31/2014 13:49:23",
"Comment": "",
"CreatedByUser": "User89",
"IsSrNoAvailable": "N",
"CommentType": "External",
"Notification": "N",
"FeedbackSRType": "",
"IntegrationId": "1420033765921",
"Date1": "",
"Date2": "",
"Date3": "",
"Text1": "",
"ListOfDataSrNotesAuditTrail": {}
}
]
},
"ListOfDataSubscribeDuplicateSr": {
"DataSubscribeDuplicateSr": [
{
"Activeflag": "Y",
"EmailId": "pratik.desai#yoopmail.com",
"Name": "010420150405",
"Type": "Subscription",
"LastUpdatedBy": ""
}
]
},
"ListOfChildServiceRequest": {},
"ListOfDataBillingCsscAdjustment": {},
"ListOfDataBillingEccAdjustment": {},
"ListOfDataBillingRsscAdjustment": {},
"ListOfDataBillingRsscExemption": {},
"ListOfDataSanitationBillingBif": {},
"ListOfDataSanitationBillingCssc": {},
"ListOfDataSanitationBillingEcc": {},
"ListOfDataSanitationBillingInquiry": {},
"ListOfDataSanitationBillingLifeline": {},
"ListOfDataSanitationBillingRssc": {},
"ListOfDataSanitationBillingSrf": {},
"ListOfDataDocumentLog": {},
"ListOfAuditTrailItem2": {},
"ListOfDataGenericBc": {
"DataGenericBc": [
{
"ATTRIB_08": "",
"NAME": "41572025-3803-49c4-8561-6e7ef41775df",
"PAR_ROW_ID": "1-N607",
"ROW_ID": "1-N60A",
"TYPE": "GIS",
"ListOfDataGenericbcAuditTrail": {}
},
{
"ATTRIB_08": "",
"NAME": "a5b5b2b9-d2e7-400a-bf75-1138ff013caa",
"PAR_ROW_ID": "1-N607",
"ROW_ID": "1-N609",
"TYPE": "Homeless Encampment",
"ListOfDataGenericbcAuditTrail": {}
},
{
"ATTRIB_08": "",
"NAME": "010420150405",
"PAR_ROW_ID": "1-N607",
"ROW_ID": "1-RN2D",
"TYPE": "Subscription",
"ListOfDataGenericbcAuditTrail": {}
}
]
},
"ListOfDataServiceNotComplete": {},
"ListOfDataOther": {},
"ListOfDataWeedAbatementForPrivateParcels": {}
}
]
}
}
}
Query 3 input
{
"MetaData": {},
"RequestSpecificDetail": {
"ParentSRNumberForLink": ""
},
"SRData": {
"SRNumber":"1-5968841",
"Anonymous": "N",
"Assignee": "",
"CreatedByUserLogin": "KAHUNA30DEC",
"CustomerAccessNumber": "",
"LADWPAccountNo": "",
"Language": "English",
"ListOfDataGisLayer": {},
"ListOfDataServiceRequestNotes": {
"DataServiceRequestNotes": [
{
"Comment": "description 1245",
"CommentType": "Feedback",
"CreatedByUser": "KAHUNA30DEC",
"FeedbackSRType": "Illegal Dumping in Progress",
"IsSrNoAvailable": "N"
},
{
"Comment": "comments 123568",
"CommentType": "External",
"CreatedByUser": "",
"IsSrNoAvailable": "N"
}
]
},
"LoginUser": "KAHUNA30DEC",
"MobilOS": "Android",
"NewContactEmail": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"Owner": "Other",
"ParentSRNumber": "",
"Priority": "Normal",
"SRCommunityPoliceStation": "RAMPART",
"UpdatedByUserLogin": "KAHUNA30DEC",
"Status": "Open",
"SRType": "Feedback",
"ServiceDate": "02/11/2015",
"Source": "Mobile App"
}
}
Query 3 output
{
"status": {
"code": 311,
"message": "Service Request Successfully Submited",
"cause": ""
},
"Response": {
"PrimaryRowId": "1-3JXL5",
"ListOfServiceRequest": {
"ServiceRequest": [
{
"SRNumber": "1-5968841"
}
]
}
}
}
Python Script responds with query two output
import json
import jsonpickle
import arcpy
import json
import numpy
import requests
f = open('C:\Users\Administrator\Desktop\myData.json', 'r')
data = jsonpickle.encode( jsonpickle.decode(f.read()) )
url = "https://myDatatest.lacity.org/myDatarouter/srbe/1/QuerySR"
headers = {'Content-type': 'text/plain', 'Accept': '/'}
r = requests.post(url, data=data, headers=headers)
sr = arcpy.SpatialReference(4326)
decoded = json.loads(r.text)
# pretty printing of json-formatted string
print json.dumps(decoded, sort_keys=True, indent=4)
f.close()
decoded is a dictionary containing the data you're looking for.
try:
print decoded
print decoded.keys()
print decoded.items()

Parsing HTML does not output desired data(tracking info for FedEx)

Im trying to make a script that grabs tracking information from fedex website.
I figured that f i just go to the url 'https://www.fedex.com/fedextrack/?tracknumbers=' and paste the tracking number at the end of it, it brings me to the tracking page which has the information i need.
I tried to feed the URL the tracking number and parse the html from the response.
This is what I tried.
import urllib
url_prefix= 'https://www.fedex.com/fedextrack/?tracknumbers='
tracking_number = '570573906561'
url = url_prefix + tracking_number
sock = urllib.urlopen(url) htmlSource = sock.read()
sock.close()
print htmlSource
this code outputs:
http://freetexthost.com/iy1ma2q1fm
I thought i would just be able to search the text from the output and find the delivery status/date but it is not in this output.
If i go to the tracking page in Chrome and inspect element, I see that the delivery date information has an id of destionDateTime,
so if i run this in the Chrome Console:
var document.getElementbyID('destinationDateTime')
it returns the output I want (delivery date)
How come my python script doesn't print actual tracking data information or that class in the html output?
I tried searching this question and tried parsing several different ways (Mechanize, Beautiful Soup, html2text) but all of these gave me the same output that does not contain any actual data about the shipment.
The website, like many others, won't work without JavaScript. It sends a HTTP POST request to a certain URL, which then returns the tracking data as a JSON-encoded object.
You'll need to simulate that with Python:
import requests
import json
tracking_number = '570573906561'
data = requests.post('https://www.fedex.com/trackingCal/track', data={
'data': json.dumps({
'TrackPackagesRequest': {
'appType': 'wtrk',
'uniqueKey': '',
'processingParameters': {
'anonymousTransaction': True,
'clientId': 'WTRK',
'returnDetailedErrors': True,
'returnLocalizedDateTime': False
},
'trackingInfoList': [{
'trackNumberInfo': {
'trackingNumber': tracking_number,
'trackingQualifier': '',
'trackingCarrier': ''
}
}]
}
}),
'action': 'trackpackages',
'locale': 'en_US',
'format': 'json',
'version': 99
}).json()
And then work with the resulting object:
{
"TrackPackagesResponse": {
"successful": true,
"passedLoggedInCheck": false,
"errorList": [{
"code": "0",
"message": "Request was successfully processed.",
"source": null
}],
"packageList": [{
"trackingNbr": "570573906561",
"trackingQualifier": "2456536000\u007e570573906561\u007eFX",
"trackingCarrierCd": "FDXE",
"trackingCarrierDesc": "FedEx Express",
"displayTrackingNbr": "570573906561",
"shipperCmpnyName": "",
"shipperName": "",
"shipperAddr1": "",
"shipperAddr2": "",
"shipperCity": "SEOUL",
"shipperStateCD": "",
"shipperZip": "",
"shipperCntryCD": "KR",
"shipperPhoneNbr": "",
"shippedBy": "",
"recipientCmpnyName": "",
"recipientName": "",
"recipientAddr1": "",
"recipientAddr2": "",
"recipientCity": "CHEK LAP KOK",
"recipientStateCD": "",
"recipientZip": "",
"recipientCntryCD": "HK",
"recipientPhoneNbr": "",
"shippedTo": "",
"keyStatus": "Delivered",
"keyStatusCD": "DL",
"lastScanStatus": "",
"lastScanDateTime": "",
"receivedByNm": ".CHOP",
"subStatus": "Signed for by\u003a .CHOP",
"mainStatus": "",
"statusBarCD": "DL",
"shortStatus": "",
"shortStatusCD": "",
"statusLocationAddr1": "",
"statusLocationAddr2": "",
"statusLocationCity": "CHEK LAP KOK",
"statusLocationStateCD": "",
"statusLocationZip": "",
"statusLocationCntryCD": "HK",
"statusWithDetails": "Delivered\u003a 9\u002f02\u002f2013 11\u003a58 am Signed for by\u003a.CHOP\u003b CHEK LAP KOK, HK",
"shipDt": "2013\u002d08\u002d31T15\u003a00\u003a00\u002b09\u003a00",
"displayShipDt": "8\u002f31\u002f2013",
"displayShipTm": "3\u003a00 pm",
"displayShipDateTime": "8\u002f31\u002f2013 3\u003a00 pm",
"pickupDt": "2013\u002d08\u002d31T15\u003a00\u003a00\u002b09\u003a00",
"displayPickupDt": "8\u002f31\u002f2013",
"displayPickupTm": "3\u003a00 pm",
"displayPickupDateTime": "8\u002f31\u002f2013 3\u003a00 pm",
"estDeliveryDt": "",
"estDeliveryTm": "",
"displayEstDeliveryDt": "",
"displayEstDeliveryTm": "",
"displayEstDeliveryDateTime": "",
"actDeliveryDt": "2013\u002d09\u002d02T11\u003a58\u003a00\u002b08\u003a00",
"displayActDeliveryDt": "9\u002f02\u002f2013",
"displayActDeliveryTm": "11\u003a58 am",
"displayActDeliveryDateTime": "9\u002f02\u002f2013 11\u003a58 am",
"nickName": "",
"note": "",
"matchedAccountList": [""],
"fxfAdvanceETA": "",
"fxfAdvanceReason": "",
"fxfAdvanceStatusCode": "",
"fxfAdvanceStatusDesc": "",
"destLink": "",
"originLink": "",
"hasBillOfLadingImage": false,
"hasBillPresentment": false,
"signatureRequired": 0,
"totalKgsWgt": "3.5",
"displayTotalKgsWgt": "3.5 kgs",
"totalLbsWgt": "7.8",
"displayTotalLbsWgt": "7.8 lbs",
"displayTotalWgt": "7.8 lbs \u002f 3.5 kgs",
"pkgKgsWgt": "3.5",
"displayPkgKgsWgt": "3.5 kgs",
"pkgLbsWgt": "7.8",
"displayPkgLbsWgt": "7.8 lbs",
"displayPkgWgt": "7.8 lbs \u002f 3.5 kgs",
"dimensions": "20x14x14 in.",
"masterTrackingNbr": "",
"masterQualifier": "",
"masterCarrierCD": "",
"originalOutboundTrackingNbr": null,
"originalOutboundQualifier": "",
"originalOutboundCarrierCD": "",
"invoiceNbrList": [""],
"referenceList": [""],
"doorTagNbrList": [""],
"referenceDescList": [""],
"purchaseOrderNbrList": [""],
"billofLadingNbrList": [""],
"shipperRefList": ["PO\u00232612 Proton housing\u005fPlastics"],
"rmaList": [""],
"deptNbrList": [""],
"shipmentIdList": [""],
"tcnList": [""],
"partnerCarrierNbrList": [""],
"hasAssociatedShipments": false,
"hasAssociatedReturnShipments": false,
"assocShpGrp": 0,
"drTgGrp": ["0"],
"associationInfoList": [{
"trackingNumberInfo": {
"trackingNumber": "",
"trackingQualifier": "",
"trackingCarrier": "",
"processingParameters": null
},
"associatedType": ""
}],
"returnReason": "",
"returnRelationship": null,
"skuItemUpcCdList": [""],
"receiveQtyList": [""],
"itemDescList": [""],
"partNbrList": [""],
"serviceCD": "INTERNATIONAL\u005fPRIORITY",
"serviceDesc": "FedEx International Priority",
"serviceShortDesc": "IP",
"packageType": "YOUR\u005fPACKAGING",
"packaging": "Your Packaging",
"clearanceDetailLink": "",
"showClearanceDetailLink": false,
"manufactureCountryCDList": [""],
"commodityCDList": [""],
"commodityDescList": [""],
"cerNbrList": [""],
"cerComplaintCDList": [""],
"cerComplaintDescList": [""],
"cerEventDateList": [""],
"displayCerEventDateList": [""],
"totalPieces": "1",
"specialHandlingServicesList": ["Deliver Weekday", "Weekend Pick\u002dUp"],
"shipmentType": "",
"pkgContentDesc1": "",
"pkgContentDesc2": "",
"docAWBNbr": "",
"originalCharges": "",
"transportationCD": "",
"transportationDesc": "",
"dutiesAndTaxesCD": "",
"dutiesAndTaxesDesc": "",
"origPieceCount": "",
"destPieceCount": "",
"goodsClassificationCD": "",
"receipientAddrQty": "0",
"deliveryAttempt": "0",
"codReturnTrackNbr": "",
"scanEventList": [{
"date": "2013\u002d09\u002d02",
"time": "11\u003a58\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "Delivered",
"statusCD": "DL",
"scanLocation": "CHEK LAP KOK HK",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": true
}, {
"date": "2013\u002d09\u002d02",
"time": "09\u003a36\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "On FedEx vehicle for delivery",
"statusCD": "OD",
"scanLocation": "LANTAU ISLAND HK",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d02",
"time": "08\u003a55\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "At local FedEx facility",
"statusCD": "AR",
"scanLocation": "LANTAU ISLAND HK",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d02",
"time": "07\u003a12\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "International shipment release \u002d Import",
"statusCD": "CC",
"scanLocation": "LANTAU ISLAND HK",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d02",
"time": "04\u003a40\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "Shipment exception",
"statusCD": "SE",
"scanLocation": "GUANGZHOU CN",
"scanDetails": "Delay beyond our control",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d02",
"time": "03\u003a45\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "Departed FedEx location",
"statusCD": "DP",
"scanLocation": "GUANGZHOU CN",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d02",
"time": "01\u003a17\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "Arrived at FedEx location",
"statusCD": "AR",
"scanLocation": "GUANGZHOU CN",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d01",
"time": "23\u003a10\u003a00",
"gmtOffset": "\u002b08\u003a00",
"status": "In transit",
"statusCD": "IT",
"scanLocation": "SHANGHAI CN",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d09\u002d01",
"time": "17\u003a13\u003a00",
"gmtOffset": "\u002b09\u003a00",
"status": "In transit",
"statusCD": "IT",
"scanLocation": "INCHEON KR",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d08\u002d31",
"time": "19\u003a44\u003a00",
"gmtOffset": "\u002b09\u003a00",
"status": "In transit",
"statusCD": "IT",
"scanLocation": "INCHEON KR",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d08\u002d31",
"time": "16\u003a27\u003a00",
"gmtOffset": "\u002b09\u003a00",
"status": "Left FedEx origin facility",
"statusCD": "DP",
"scanLocation": "SEOUL KR",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d08\u002d31",
"time": "15\u003a00\u003a00",
"gmtOffset": "\u002b09\u003a00",
"status": "Picked up",
"statusCD": "PU",
"scanLocation": "SEOUL KR",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}, {
"date": "2013\u002d08\u002d30",
"time": "23\u003a58\u003a11",
"gmtOffset": "\u002d05\u003a00",
"status": "Shipment information sent to FedEx",
"statusCD": "OC",
"scanLocation": "",
"scanDetails": "",
"scanDetailsHtml": "",
"rtrnShprTrkNbr": "",
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": false
}],
"originAddr1": "",
"originAddr2": "",
"originCity": "SEOUL",
"originStateCD": "",
"originZip": "",
"originCntryCD": "KR",
"originLocationID": "",
"originTermCity": "SEOUL",
"originTermStateCD": "",
"destLocationAddr1": "",
"destLocationAddr2": "",
"destLocationCity": "LANTAU ISLAND",
"destLocationStateCD": "",
"destLocationZip": "",
"destLocationCntryCD": "HK",
"destLocationID": "",
"destLocationTermCity": "LANTAU ISLAND",
"destLocationTermStateCD": "",
"destAddr1": "",
"destAddr2": "",
"destCity": "CHEK LAP KOK",
"destStateCD": "",
"destZip": "",
"destCntryCD": "HK",
"halAddr1": "",
"halAddr2": "",
"halCity": "",
"halStateCD": "",
"halZipCD": "",
"halCntryCD": "",
"actualDelAddrCity": "CHEK LAP KOK",
"actualDelAddrStateCD": "",
"actualDelAddrZipCD": "",
"actualDelAddrCntryCD": "HK",
"totalTransitMiles": "",
"excepReasonList": [""],
"excepActionList": [""],
"exceptionReason": "",
"exceptionAction": "",
"statusDetailsList": [""],
"trackErrCD": "",
"destTZ": "\u002b08\u003a00",
"originTZ": "\u002b09\u003a00",
"isMultiStat": "0",
"multiStatList": [{
"multiPiec": "",
"multiTm": "",
"multiDispTm": "",
"multiSta": ""
}],
"maskMessage": "",
"deliveryService": "",
"milestoDestination": "",
"terms": "",
"originUbanizationCode": "",
"originCountryName": "",
"isOriginResidential": false,
"halUrbanizationCD": "",
"halCountryName": "",
"actualDelAddrUrbanizationCD": "",
"actualDelAddrCountryName": "",
"destUrbanizationCD": "",
"destCountryName": "",
"delToDesc": "Shipping\u002fReceiving",
"recpShareID": "",
"shprShareID": "9mbo6hrq0tqxo1i4pr7kp2yp",
"defaultCDOType": "CDO",
"mpstype": "",
"fxfAdvanceNotice": true,
"rthavailableCD": "",
"excepReasonListNoInit": [""],
"excepActionListNoInit": [""],
"statusDetailsListNoInit": [""],
"matched": false,
"isSuccessful": true,
"errorList": [{
"code": "",
"message": "",
"source": null
}],
"isCanceled": false,
"isPrePickup": false,
"isPickup": false,
"isInTransit": false,
"isInProgress": true,
"isDelException": false,
"isClearanceDelay": false,
"isException": false,
"isDelivered": true,
"isHAL": false,
"isOnSchedule": false,
"isDeliveryToday": false,
"isSave": false,
"isWatch": false,
"isHistorical": false,
"isTenderedNotification": false,
"isDeliveredNotification": true,
"isExceptionNotification": false,
"isCurrentStatusNotification": false,
"isAnticipatedShipDtLabel": false,
"isShipPickupDtLabel": true,
"isActualPickupLabel": false,
"isOrderReceivedLabel": false,
"isEstimatedDeliveryDtLabel": true,
"isDeliveryDtLabel": false,
"isActualDeliveryDtLabel": true,
"isOrderCompleteLabel": false,
"isOutboundDirection": false,
"isInboundDirection": false,
"isThirdpartyDirection": false,
"isUnknownDirection": false,
"isFSM": false,
"isReturn": false,
"isOriginalOutBound": false,
"isChildPackage": false,
"isParentPackage": false,
"isReclassifiedAsSingleShipment": false,
"isDuplicate": false,
"isMaskShipper": false,
"isHalEligible": false,
"isFedexOfficeOnlineOrders": false,
"isFedexOfficeInStoreOrders": false,
"isMultipleStop": false,
"isCustomCritical": false,
"isInvalid": false,
"isNotFound": false,
"isFreight": false,
"isSpod": true,
"isSignatureAvailable": false,
"isMPS": false,
"isGMPS": false,
"isResidential": false,
"isDestResidential": true,
"isHALResidential": false,
"isActualDelAddrResidential": false,
"isReqEstDelDt": false,
"isCDOEligible": false,
"CDOInfoList": [{
"spclInstructDesc": "",
"delivOptn": "",
"delivOptnStatus": "",
"reqApptWdw": "",
"reqApptDesc": "",
"rerouteTRKNbr": "",
"beginTm": "",
"endTm": ""
}],
"CDOExists": false,
"isMtchdByRecShrID": false,
"isMtchdByShiprShrID": false
}]
}
}
this is what I ended up with, thanks to #Blender
import requests
import json
daysdict = {1:31,2:28,3:31,4:31,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
def days_in_month(month):
for key, value in daysdict.iteritems():
if key == month:
number_of_days = value
return number_of_days
def build_output(tracking_number):
data = requests.post('https://www.fedex.com/trackingCal/track', data={
'data': json.dumps({
'TrackPackagesRequest': {
'appType': 'wtrk',
'uniqueKey': '',
'processingParameters': {
'anonymousTransaction': True,
'clientId': 'WTRK',
'returnDetailedErrors': True,
'returnLocalizedDateTime': False
},
'trackingInfoList': [{
'trackNumberInfo': {
'trackingNumber': tracking_number,
'trackingQualifier': '',
'trackingCarrier': ''
}
}]
}
}),
'action': 'trackpackages',
'locale': 'en_US',
'format': 'json',
'version': 99
}).json()
return data
# finds delivery date info
ship_arrival_key = 'displayActDeliveryDateTime'
ship_time_key = 'displayShipDt'
def track(tracking_number):
data = build_output(tracking_number)
#narrowing down dictionary and lists to objects needed (ship day,arrival)
for key, value in data.iteritems():
narrow = value
#narrow more into packageList list
for key, value in narrow.iteritems():
if key == 'packageList':
narrow = value
# narrow to ship start value
for x, y in narrow[0].iteritems():
if x == ship_arrival_key:
ship_arival_value = y
exists = True
# also find ship arrival
elif x == ship_time_key:
ship_time_value = y
exists = True
# list with two items shiptime and shiparrival
return ship_time_value, ship_arival_value, exists
def print_results(tracking_number):
to_fro = track(tracking_number)
if to_fro[2] == True:
try:
daysinmonth = days_in_month(int(to_fro[0][0]))
try:
if to_fro[0][0] != to_fro[1][0]:
ship_days = str( (int(daysinmonth) - int(str((to_fro[0][2]))+str((to_fro[0][3]))) + int(to_fro[1][3])) )
print '_____________________'
print 'Shipped: ' + to_fro[0]
print 'Arrived: ' + to_fro[1]
print '_____________________'
print '\nShipping took:' +" " +ship_days
else:
ship_days = int(to_fro[1][2] + to_fro[1][3]) - int(to_fro[0][2] + to_fro[0][3])
print '_____________________'
print 'Shipped: ' + to_fro[0]
print 'Arrived: ' + to_fro[1]
print '_____________________'
print '\nShipping took:' +" " + str(ship_days)
except IndexError:
print 'Invalid Tracking Number'
pass
except IndexError:
pass
else:
pass
def raw_results(tracking_number):
to_fro = track(tracking_number)
if to_fro[2] == True:
daysinmonth = days_in_month(int(track(tracking_number)[0][0]))
try:
if to_fro[0][0] != to_fro[1][0]:
ship_days = str( (int(daysinmonth) - int(str((to_fro[0][2]))+str((to_fro[0][3]))) + int(to_fro[1][3])) )
else:
ship_days = int(to_fro[1][2] + to_fro[1][3]) - int(to_fro[0][2] + to_fro[0][3])
except IndexError:
print 'Invalid Tracking Number'
pass
else:
pass
return ship_days
#print_results(499552080632881)

Categories

Resources