have Python script that parses a JSON web-service and writes to a geodatabase. I would like to be able to create an if statement for fields that are in the web-service so if they contain values, then they are written to my table, if not, no value is added, but the field is still there, i.e. if there is no bulky item a bulkyitem field is added with no value or bulkyitemqty no value is added, and if values are there the fields are written and values are added. My script works now because I know which fields and values are available in the test web-service. Now, I am writing the first electronic waste object to my database. Ideally, I would like to iterate through most of the fields shown in the JSON response(not all) and write those to my table as well.
Script:
import json
import jsonpickle
import requests
import arcpy
import numpy
fc = "C:\MYLATesting.gdb\MYLA311"
if arcpy.Exists(fc):
arcpy.Delete_management(fc)
f = open('C:\Users\Administrator\Desktop\myla311.json', 'r')
data = jsonpickle.encode( jsonpickle.decode(f.read()) )
url = "myURL"
headers = {'Content-type': 'text/plain', 'Accept': '/'}
r = requests.post(url, data=data, headers=headers)
sr = arcpy.SpatialReference(4326)
decoded = json.loads(r.text)
SRAddress = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['SRAddress']
latitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Latitude']
longitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Longitude']
CommodityType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['Type']
ItemType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ElectronicWestType']
ItemCount_1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ItemCount']
CommodityType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['Type']
ItemType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ElectronicWestType']
ItemCount1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ItemCount']
print SRAddress
print latitude
print longitude
print CommodityType
print ItemType
print ItemCount_1
print CommodityType1
print ItemType1
print ItemCount1
#Items and Keys should maintain same order
item ={'SRAddress': SRAddress, 'CommodityType': CommodityType, 'ItemType': ItemType, 'ItemCount_1': ItemCount_1, 'longitude': longitude, 'latitude': latitude}
import numpy as np #NOTE THIS
keys = ['SRAddress','CommodityType', 'ItemType','ItemCount_1','longitude','latitude']
k1,k2,k3, k4, k5, k6 = keys
# data_line ={'SRAddress': SRAddress, 'Longitude': longitude, 'Latitude': latitude, 'CommodityType': CommodityType, 'ItemCount': ItemCount}
frmt = '\nStraight dictionary output\n Address: {} Long: {} Lat: {}'
print(frmt.format(item[k1],item[k2],item[k3], item[k4],item[k5], item[k6]))
print '\noption 1: List comprehension with unicode'
a = tuple([unicode(item[key]) for key in keys]) # list comprehension with unicode
print('{}'.format(a))
dt = np.dtype([('SRAddress','U40'),('CommodityType','U40'), ('ItemType','U40'), ('ItemCount_1','U40'),('longitude','<f8'),('latitude','<f8')])
arr = np.array(a,dtype=dt)
# print'\narray unicode\n',arr
# print'dtype',arr.dtype
# print '\noption 2:List comprehension without unicode'
# b = tuple([item[key] for key in keys])
# print('{}'.format(b))
# dt = np.dtype([('SRAddress','U40'),('CommodityType','S4'), ('ItemCount','U40'),('longitude','<f8'),('latitude','<f8')])
# arr = np.array(b,dtype=dt)
# print'\narray without unicode\n',arr
# print'dtype',arr.dtype
arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitude', 'latitude'], sr)
Output:
'5810 N WILLIS AVE, 91411
34.176277
-118.455249
Electronic Waste
Microwaves
3
Electronic Waste
Televisions (Any Size)
6
Straight dictionary output
Address: 5810 N WILLIS AVE, 91411 Long: Electronic Waste Lat: Microwaves
option 1: List comprehension with unicode
(u'5810 N WILLIS AVE, 91411', u'Electronic Waste', u'Microwaves', u'3', u'-118.455249', u'34.176277')`
Output JSON:
{
"status": {
"code": 311,
"message": "Service Request Successfully Queried.",
"cause": ""
},
"Response": {
"NumOutputObjects": "1",
"ListOfServiceRequest": {
"ServiceRequest": [
{
"AddressVerified": "Y",
"SRNumber": "1-3580171",
"SRType": "Electronic Waste",
"CreatedDate": "02/17/2015 16:53:25",
"UpdatedDate": "02/17/2015 16:53:25",
"IntegrationId": "02172015165417667",
"Status": "Open",
"CreatedByUserLogin": "MYLATHREEONEONE",
"UpdatedByUserLogin": "MYLATHREEONEONE",
"Anonymous": "N",
"Zipcode": "91411",
"Latitude": "34.176277",
"Longitude": "-118.455249",
"CustomerAccessNumber": "",
"LADWPAccountNo": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"NewContactEmail": "",
"ParentSRNumber": "",
"Priority": "Normal",
"Language": "",
"ReasonCode": "",
"ServiceDate": "02/19/2015 00:00:00",
"Source": "",
"Email": "mylathreeoneone@gmail.com",
"FirstName": "Myla",
"HomePhone": "2131234567",
"LastName": "Threeoneone",
"LoginUser": "",
"ResolutionCode": "",
"SRUnitNumber": "5810",
"MobilOS": "",
"SRAddress": "5810 N WILLIS AVE, 91411",
"SRAddressName": "",
"SRAreaPlanningCommission": "South Valley APC",
"SRCommunityPoliceStation": "VALLEY BUREAU",
"SRCouncilDistrictMember": "Tom LaBonge",
"SRCouncilDistrictNo": "4",
"SRDirection": "N",
"SRNeighborhoodCouncilId": "20",
"SRNeighborhoodCouncilName": "VAN NUYS NC",
"SRStreetName": "WILLIS",
"SRSuffix": "AVE",
"SRTBColumn": "J",
"SRTBMapGridPage": "561",
"SRTBRow": "1",
"SRXCoordinate": "6423983",
"SRYCoordinate": "1886848",
"AssignTo": "EV",
"Assignee": "",
"Owner": "BOS",
"ParentSRStatus": "",
"ParentSRType": "",
"ParentSRLinkDate": "",
"ParentSRLinkUser": "",
"SRAreaPlanningCommissionId": "3",
"SRCommunityPoliceStationAPREC": "VAN NUYS",
"SRCommunityPoliceStationPREC": "9",
"SRCrossStreet": "",
"ActionTaken": "",
"SRCity": "",
"RescheduleCounter": "",
"SRHouseNumber": "5810",
"ListOfLa311BarricadeRemoval": {},
"ListOfLa311BulkyItem": {},
"ListOfLa311DeadAnimalRemoval": {},
"ListOfLa311GraffitiRemoval": {},
"ListOfLa311InformationOnly": {},
"ListOfLa311MultipleStreetlightIssue": {},
"ListOfLa311SingleStreetlightIssue": {},
"ListOfLa311SrPhotoId": {
"La311SrPhotoId": [
{
"Name": "021720151654176671",
"PhotoId": "https://myla311.lacity.org/portal/docview?id=04b8ba678fe21d32b05673eb9ad7711b",
"Type": "SR Photo ID",
"LastUpdatedBy": ""
}
]
},
"ListOfLa311BusPadLanding": {},
"ListOfLa311CurbRepair": {},
"ListOfLa311Flooding": {},
"ListOfLa311GeneralStreetInspection": {},
"ListOfLa311GuardWarningRailMaintenance": {},
"ListOfLa311GutterRepair": {},
"ListOfLa311LandMudSlide": {},
"ListOfLa311Pothole": {},
"ListOfLa311Resurfacing": {},
"ListOfLa311SidewalkRepair": {},
"ListOfLa311StreetSweeping": {},
"ListOfLa311BeesOrBeehive": {},
"ListOfLa311MedianIslandMaintenance": {},
"ListOfLa311OvergrownVegetationPlants": {},
"ListOfLa311PalmFrondsDown": {},
"ListOfLa311StreetTreeInspection": {},
"ListOfLa311StreetTreeViolations": {},
"ListOfLa311TreeEmergency": {},
"ListOfLa311TreeObstruction": {},
"ListOfLa311TreePermits": {},
"ListOfLa311BrushItemsPickup": {},
"ListOfLa311Containers": {},
"ListOfLa311ElectronicWaste": {
"La311ElectronicWaste": [
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "Microwaves",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "3",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176711"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "Televisions (Any Size)",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "6",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176722"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "VCR/DVD Players",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "1",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176723"
}
]
},
"ListOfLa311IllegalDumpingPickup": {},
"ListOfLa311ManualPickup": {},
"ListOfLa311MetalHouseholdAppliancesPickup": {},
"ListOfLa311MoveInMoveOut": {},
"ListOfLa311HomelessEncampment": {},
"ListOfLa311IllegalAutoRepair": {},
"ListOfLa311IllegalConstruction": {},
"ListOfLa311IllegalConstructionFence": {},
"ListOfLa311IllegalDischargeOfWater": {},
"ListOfLa311IllegalDumpingInProgress": {},
"ListOfLa311IllegalExcavation": {},
"ListOfLa311IllegalSignRemoval": {},
"ListOfLa311IllegalVending": {},
"ListOfLa311LeafBlowerViolation": {},
"ListOfLa311NewsRackViolation": {},
"ListOfLa311Obstructions": {},
"ListOfLa311TablesAndChairsObstructing": {},
"ListOfLa311GisLayer": {
"La311GisLayer": [
{
"A_Call_No": "",
"Area": "0",
"Day": "THURSDAY",
"DirectionSuffix": "",
"DistrictAbbr": "",
"DistrictName": "EV",
"DistrictNumber": "",
"DistrictOffice": "",
"Fraction": "",
"R_Call_No": "",
"SectionId": "",
"ShortDay": "Thu",
"StreetFrom": "",
"StreetTo": "",
"StreetLightId": "",
"StreetLightStatus": "",
"Type": "GIS",
"Y_Call_No": "",
"Name": "02172015165417667100",
"CommunityPlanningArea": "",
"LastUpdatedBy": "",
"BOSRadioHolderName": ""
}
]
},
"ListOfLa311ServiceRequestNotes": {
"La311ServiceRequestNotes": [
{
"CreatedDate": "02/17/2015 16:53:26",
"Comment": "Out on the sidewalk near the curb. Hopefully it is still there.",
"CreatedByUser": "MYLATHREEONEONE",
"IsSrNoAvailable": "",
"CommentType": "Address Comments",
"Notification": "N",
"FeedbackSRType": "",
"IntegrationId": "021720151654176661",
"Date1": "",
"Date2": "",
"Date3": "",
"Text1": "",
"ListOfLa311SrNotesAuditTrail": {}
},
{
"CreatedDate": "02/17/2015 16:53:26",
"Comment": "So glad to get rid of this old junk. Thanks.",
"CreatedByUser": "MYLATHREEONEONE",
"IsSrNoAvailable": "",
"CommentType": "External",
"Notification": "N",
"FeedbackSRType": "",
"IntegrationId": "021720151654176662",
"Date1": "",
"Date2": "",
"Date3": "",
"Text1": "",
"ListOfLa311SrNotesAuditTrail": {}
}
]
},
"ListOfLa311SubscribeDuplicateSr": {},
"ListOfChildServiceRequest": {},
"ListOfLa311BillingCsscAdjustment": {},
"ListOfLa311BillingEccAdjustment": {},
"ListOfLa311BillingRsscAdjustment": {},
"ListOfLa311BillingRsscExemption": {},
"ListOfLa311SanitationBillingBif": {},
"ListOfLa311SanitationBillingCssc": {},
"ListOfLa311SanitationBillingEcc": {},
"ListOfLa311SanitationBillingInquiry": {},
"ListOfLa311SanitationBillingLifeline": {},
"ListOfLa311SanitationBillingRssc": {},
"ListOfLa311SanitationBillingSrf": {},
"ListOfLa311DocumentLog": {},
"ListOfAuditTrailItem2": {},
"ListOfLa311GenericBc": {
"La311GenericBc": [
{
"ATTRIB_08": "N",
"NAME": "021720151654176711",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHR",
"TYPE": "Electronic Waste",
"ListOfLa311GenericbcAuditTrail": {}
},
{
"ATTRIB_08": "N",
"NAME": "021720151654176722",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHS",
"TYPE": "Electronic Waste",
"ListOfLa311GenericbcAuditTrail": {}
},
{
"ATTRIB_08": "N",
"NAME": "021720151654176723",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHT",
"TYPE": "Electronic Waste",
"ListOfLa311GenericbcAuditTrail": {}
},
{
"ATTRIB_08": "",
"NAME": "02172015165417667100",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHU",
"TYPE": "GIS",
"ListOfLa311GenericbcAuditTrail": {}
},
{
"ATTRIB_08": "",
"NAME": "021720151654176671",
"PAR_ROW_ID": "1-24QH7",
"ROW_ID": "1-24QHQ",
"TYPE": "SR Photo ID",
"ListOfLa311GenericbcAuditTrail": {}
}
]
},
"ListOfLa311ServiceNotComplete": {},
"ListOfLa311Other": {},
"ListOfLa311WeedAbatementForPrivateParcels": {}
}
]
}
}
}