skip to Main Content

I’m calling an API and I get the following response:

"items": [
                    {
                        "packagingType": "PCS",
                        "description": "[REDACTED]",
                        "productCode": "MATT",
                        "freightClass": "300",
                        "insuranceValue": 428.70999999971326,
                        "primaryReferenceNumber": null,
                        "poNumber": "[REDACTED]",
                        "skuNumber": "[REDACTED]",
                        "pluNumber": null,
                        "actualQuantity": 1,
                        "actualWeight": 125,
                        "actualWeightUnitOfMeasure": "Pounds",
                        "actualPallets": 1,
                        "actualVolume": null,
                        "actualVolumeUnitOfMeasure": "Cubic Feet",
                        "actualPackagingLength": null,
                        "actualPackagingHeight": null,
                        "actualPackagingWidth": null,
                        "actualPackagingUnitOfMeasure": "Inches",
                        "referenceNumbers": [
                            {
                                "type": "CustomerItemIdentifier1",
                                "value": "[REDACTED]"
                            },
                            {
                                "type": "OrderItemId",
                                "value": "123456789"
                            },
                            {
                                "type": "SHID",
                                "value": "[REDACTED]"
                            }
                        ],
                        "associatedStop": {
                            "pickSequenceNumber": null,
                            "dropSequenceNumber": null
                        }
                    }
                ]

But sometimes the API also sends an answer like this:

"items": [
                    {
                        "packagingType": "PCS",
                        "description": "[REDACTED]",
                        "productCode": "MISC",
                        "freightClass": "300",
                        "insuranceValue": 2500,
                        "primaryReferenceNumber": null,
                        "poNumber": "[REDACTED]",
                        "skuNumber": "[REDACTED]",
                        "pluNumber": null,
                        "actualQuantity": 1,
                        "actualWeight": 132,
                        "actualWeightUnitOfMeasure": "Pounds",
                        "actualPallets": null,
                        "actualVolume": null,
                        "actualVolumeUnitOfMeasure": "Cubic Feet",
                        "actualPackagingLength": null,
                        "actualPackagingHeight": null,
                        "actualPackagingWidth": null,
                        "actualPackagingUnitOfMeasure": "Inches",
                        "referenceNumbers": [
                            {
                                "type": "OrderItemId",
                                "value": "987654321"
                            }
                        ],
                        "associatedStop": {
                            "pickSequenceNumber": null,
                            "dropSequenceNumber": null
                        }
                    }

Now, what I’m trying to achieve is to append the "value" under the "referenceNumbers" when the "type" is equal to "OrderItemId".

Can anyone please point me out on the right direction to do it?

UPDATE: This is my current block of code:

 for object in parsed['results']:
            if  object['event']['eventType'] in event_list:
                for item in object['event']['items']:
                    orders_event_name.append(object['event']['eventType'])
                    orders_customer_order.append(object['billToReferenceNumber'])
                    orders_reference_number.append(item['referenceNumbers']['value'])
                    orders_event_id.append(None)
                    orders_item_id.append(item['skuNumber'])
                    orders_datetime_started.append(object['eventTime'])
                    orders_seconds.append(None)
                    orders_load_number.append(object['event']['loadNumber'])

2

Answers


  1. li = []
    for resp in [response1, response2]:
        for i in resp['items'][0]['referenceNumbers']:
            if i['type'] == 'OrderItemId':
                li.append(i['value'])
    
    li
    

    Output:

    ['123456789', '987654321']
    
    Login or Signup to reply.
  2. An answer with your code:

    for i in data:
        if i["referenceNumbers"][0]["type"] == "OrderItemId":
            orders_reference_number.append(i["referenceNumbers"][0]["value"])
            orders_event_id.append(None)
            orders_item_id.append(i['skuNumber'])
    
        else:
            print("Not OrderItemId")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search