0
votes

I have a json which can contain up to 10 nested dictionaries within a list. These 10 nested dictionaries are all associated with one service request number, but the request per dictionary differs. For example service request 1 at address one might contain aList of [Containers] and in this list of containers{key1:value1, key2, value2, key3:value3} and so on, up to 10 times.

How do I successfully append these values to one field based on the number it occurs in the dictionary, i.e. value1 = containercolor1, value2 = containercolor2, value3 = containercolor3? See example below, this same example holds true to multiple fields in the below json that I am working with.

I basically want to access each key/value pair in each list, for instance

for k_container in k_container_number_1:
      print k_container[0]

Prints:

`R 9 G 0 7 8 7 7 8 6 8 7 5 R 9 B 0 7 8 7 7 8 6 7 5 5 R 6 B 1 2 3 4 8 7 8 7 6 7

r 9 b 0 5 3 0 2 2`

How can I fix this so that my characters aren't looped or horizontal?

Example of JSON:

  {
    "Response": {
        "LastPage": "false",
        "NumOutputObjects": "1",
        "ListOfServiceRequest": {
            "ServiceRequest": [
                {
                    "AddressVerified": "Y",
                    "SRNumber": "1-20229961",
                    "ListOfLa311Containers": {
                        "La311Containers": [
                            {
                                "ContainerNumber": "R9G0665765765",
                                "ContainerType": "Green Yard Trimmings",
                                "DamageonLid": "N",
                                "DamageonAxle": "N",
                                "DamageonBody": "N",
                                "DamageonWheels": "Y",
                                "DeliveryReason": "",
                                "DriverFirstName": "",
                                "DriverLastName": "",
                                "ExchangeDetails": "",
                                "GatedCommunityMultifamilyDwelling": "",
                                "MobileHomeSpace": "",
                                "PickupReason": "",
                                "RequestFor": "Damage",
                                "ServiceDateRendered": "",
                                "TruckNo": "",
                                "Type": "Containers",
                                "CollectionLocation": "Curb",
                                "ContainerSize": "90 Gallon (Large) Green",
                                "LastUpdatedBy": "370758",
                                "ActiveStatus": "Y",
                                "PurposeofSR": "",
                                "DamageonHandleEndcap": "N",
                                "ContactFirstName": "Shawnae",
                                "ContactLastName": "Muck",
                                "Name": "062520151201141851"
                            },
                            {
                                "ContainerNumber": "R9R0877765456",
                                "ContainerType": "Blue Recycling",
                                "DamageonLid": "Y",
                                "DamageonAxle": "N",
                                "DamageonBody": "Y",
                                "DamageonWheels": "N",
                                "DeliveryReason": "",
                                "DriverFirstName": "",
                                "DriverLastName": "",
                                "ExchangeDetails": "",
                                "GatedCommunityMultifamilyDwelling": "",
                                "MobileHomeSpace": "",
                                "PickupReason": "",
                                "RequestFor": "Damage",
                                "ServiceDateRendered": "",
                                "TruckNo": "",
                                "Type": "Containers",
                                "CollectionLocation": "Curb",
                                "ContainerSize": "90 Gallon (Large) Blue",
                                "LastUpdatedBy": "370758",
                                "ActiveStatus": "Y",
                                "PurposeofSR": "",
                                "DamageonHandleEndcap": "N",
                                "ContactFirstName": "Shawnae",
                                "ContactLastName": "Muck",
                                "Name": "062520151201141852"
                            },
                            {
                                "ContainerNumber": "R9B0746754645",
                                "ContainerType": "Black Refuse",
                                "DamageonLid": "N",
                                "DamageonAxle": "N",
                                "DamageonBody": "N",
                                "DamageonWheels": "N",
                                "DeliveryReason": "",
                                "DriverFirstName": "",
                                "DriverLastName": "",
                                "ExchangeDetails": "",
                                "GatedCommunityMultifamilyDwelling": "",
                                "MobileHomeSpace": "",
                                "PickupReason": "",
                                "RequestFor": "Damage",
                                "ServiceDateRendered": "",
                                "TruckNo": "",
                                "Type": "Containers",
                                "CollectionLocation": "Curb",
                                "ContainerSize": "90 Gallon (Large) Black",
                                "LastUpdatedBy": "370758",
                                "ActiveStatus": "Y",
                                "PurposeofSR": "",
                                "DamageonHandleEndcap": "Y",
                                "ContactFirstName": "Shawnae",
                                "ContactLastName": "Muck",
                                "Name": "062520151201141853"
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Current code to append to NumPy Array:

Containerdt = np.dtype([('Address', 'U128'),
                                ('Y_CoordShape', '<f8'),
                                ('X_CoordShape', '<f8'),
                                ('Y_COR', '<f8'),
                                ('X_COR', '<f8'),
                                ('ReasonCode','U128'),
                                ('NUMBERCYLA', 'U128'),
                                ('SRNumber', 'U128'),
                                ('Name', 'U128'),
                                ('RESOLUTION_CODE','U128'),
                               ('HOME_PHONE', 'U40'),
                                ('CreatedDate', 'U128'),
                                ('UpdatedDate', 'U128'),
                                # ('ItemDesc', 'U128'),
                                ('SCHED_DATE', 'U128'),
                                (ContainerNumber01, 'U128'), 
                                (ContainerNumber02, 'U128'), 
                                (ContainerNumber03, 'U128'), 

])



containerItems.append((ContainerAddress,
             container_x,
             container_y,
              container_x,
              container_y,
              Container_ReasonCode,
              Container_SRNumber,
              Container_SRNumber,
             Container_FullName,
              Container_ResolutionCode,
              Container_HomePhone,
              Container_CreatedDate,
              Container_UpdatedDate,
            # "ItemDesc",
            date_object,
            "R9G0665765765",  #FROM 1st Dictionary under ['ListofLa311Containers']['La311Containers']

             "R9R0877765456", #FROM 2nd Dictionary under ['ListofLa311Containers']['La311Containers']

             "R9B0746754645", #FROM 3RD Dictionary under ['ListofLa311Containers']['La311Containers']

))

1

1 Answers

0
votes

Is containerItems a list or array (with this complicated dtype)?

If a list, fine. If an array, you are misusing it. The syntax is wrong, and the usage context is wrong.

Without trying to figure out exactly what you are trying to do, I would suggest you write a function (or two) that takes a dictionary from the list, and picks out the desire pieces, returning them in an appropriate tuple. You can package as much logic as you need in that function.

At this stage, you should be thinking about processing dictionaries, and producing lists and/or tuples. Don't confuse yourself with the structured array business. The best way of constructing a structured array is at the end, giving np.array a list of tuples and a dtype. Do not construct it bit by bit by appending tuples.