Skip to main content

Hi Guys, 

 

I am trying to access data that is in nested JSON. I am capturing the data from webhook. Below is the parsed JSON “sBody”.

I need to extract the “Name”: “Can Coke” and “Price”: “1.5” from “OrderItemOptions” which is nested under “OrderItems”

 

{
"Type": "order.accepted",
"CreateTime": "2021-01-21T13:55:25.8773003Z",
"Body": {
"EventName": "order.accepted",
"Description": "test store accepted a Pickup order (#XXXXXXXX) for €3.99 ",
"OrderAcceptedTime": "2021-01-21T13:55:25.7209996Z",
"Order": {
"Store": {
"Id": XXXXXX,
"Name": "test store",
"MenuId": XXXXX,
"Metadata": {},
"Currency": "EUR",
"Coordinates": {
"Latitude": XXXXX,
"Longitude": -XXXX
},
"StoreTimezone": "GMT Standard Time",
"StoreGroupId": 10811
},
"Customer": {
"Id": XXXXXX,
"Name": "XXXX(TEST)",
"EmailAddress": "user@email.com",
"PhoneNumberLocalFormat": "XXXXXXXX",
"PhoneNumber": "+353XXXXXXXX"
},
"Voucher": null,
"Fees": null,
"OrderItems": e{
"OrderItemOptions": {
"Metadata": {},
"MenuItemOptionPublicId": "98151ce3-XXXXXX-7bdc2f2b0cde",
"MenuItemOptionId": XXXXXX,
"IsMasterOptionSetItem": false,
"Name": "Can coke",
"Price": 1.5,
"MenuItemOptionDisplayOrder": 0,
"MenuItemOptionSetDisplayOrder": 0
}],
"Metadata": {},
"MenuItemPublicId": "d263cea1-XXXXXXXXXXX4482674abef3",
"MenuSectionName": "Household",
"MenuSectionDisplayOrder": 0,
"Name": "Firelog",
"Description": "4 FOR 5 €",
"Price": 1.99,
"PriceIncludingOptionSetItems": 3.49,
"MenuItemId": 2277055,
"MenuItemDisplayOrder": 0,
"IsAvailable": true
}],
"DeliveryLocation": null,
"CustomerLocation": {
"Latitude": 5XXXXX,
"Longitude": XXXXX5
},
"MaskedPhoneNumber": {
"IsEnabled": true,
"PhoneNumber": "+XXXXXXXX",
"Code": "62478685"
},
"DropOffLocationId": null,
"DropOffLocation": null,
"AcceptedFor": "2021-01-21T14:40:25.72",
"InFraudZone": false,
"UnusualHighValueOrder": false,
"RejectedByUserId": null,
"OrderId": XXXXXXX,
"LocalOrderId": null,
"DeliveryType": "Pickup",
"PickupLocationType": "TakeOut",
"TableServiceCatagory": "Generic",
"TipAmount": 0.0,
"DeliveryAmount": 0.0,
"OrderItemsAmount": 3.49,
"Amount": 3.99,
"ProcessingFee": 0.5,
"PaymentAccountType": "Cash",
"PaymentAccountDescription": "Cash",
"OrderState": "AcceptedByRestaurant",
"IsPreOrder": false,
"PlacedTime": "2021-01-21T13:54:34.243",
"RequestedForTime": "2021-01-21T13:54:34.243",
"ChefNote": null,
"AppType": "Web",
"UserRating": null,
"PaymentStatus": "Paid",
"RejectionReason": null,
"RefundedAmount": null,
"DeliveryTrackingStatus": "Unassigned",
"DriverId": null,
"TotalTax": 0.16,
"OrderTrackingCode": null
},
"XXXXXXX": "cc137191-XXXXXXX-736bb36030c1",
"CreateTime": "2021-01-21T13:55:25.8773003Z",
"Position": 0,
"AppId": "XXXXXX"
}
}

I can retrieve the “OrderItems”: “Name”

By using the below, which then goes to populate a gsheet. 

 

var hBody = JSON.parse(inputData.sBody);
var hOrder = hBody.Body.Order;
var aOrderItems = hBody.Body.Order.OrderItems;

output = y];
for (const hOrderItem of aOrderItems) {
output.push({"Product name": hOrderItem.Name,
"Price": hOrderItem.Price,
"Description Parent": hOrderItem.Description,
"Order date": hOrder.AcceptedFor;
}

I thought it would be as simple as adding the below, but I am getting an unexpected error.

 

var hBody = JSON.parse(inputData.sBody);
var hOrder = hBody.Body.Order;
var aOrderItems = hBody.Body.Order.OrderItems;
var aOrderItemOptions = hBody.Body.OrderItems.OrderItemOptions; <=========
output = O];
for (const hOrderItem of aOrderItems) {
output.push({"Product name": hOrderItem.Name,
"Price": hOrderItem.Price,
"Description Parent": hOrderItem.Description,
"Order date": hOrder.AcceptedFor,
"Order Item Options":OrderItemOption.Name}); <===========
}

I’ve added the arrows to highlight they are not in my code block in code by zapier. 

I’d really appreciate any help here as I’ve hit a wall. I am not stuck to javascript either python is also usable here so anything that works will be perfectly fine for me. 

Many thanks 

Mr D

Just noticed that I had not included the actual error I am getting. Please see below.

 

TypeError: Cannot read property 'OrderItemOptions' of undefined

 


@MysterD 

From a quick glance, youve got
var aOrderItems = hBody.Body.Order.OrderItems;
var aOrderItemOptions = hBody.Body.OrderItems.OrderItemOptions;;

 

The error is referencing that second line because OrderItemOptions is nested under 
Body.Order.OrderItems.OrderItemOptions 

you are missing the Order part.

 


@MysterD 

From a quick glance, youve got
var aOrderItems = hBody.Body.Order.OrderItems;
var aOrderItemOptions = hBody.Body.OrderItems.OrderItemOptions;;

 

The error is referencing that second line because OrderItemOptions is nested under 
Body.Order.OrderItems.OrderItemOptions 

you are missing the Order part.

 

Yes, I actually noticed that earlier today and added it. Still getting the same error as above. Below is the updated code. 

 

var hBody = JSON.parse(inputData.sBody);
var hOrder = hBody.Body.Order;
var aOrderItems = hBody.Body.Order.OrderItems;
var aOrderItemOptions = hBody.Body.Order.OrderItems.OrderItemOptions;

output = t];
for (const hOrderItem of aOrderItems) {
output.push({"Product name": hOrderItem.Name,
"Price": hOrderItem.Price,
"Description Parent": hOrderItem.Description,
"Order date": hOrder.AcceptedFor,
"Order Item Options": OrderItemOptions.Name});
}

 


Ahh! We missed a crucial detail! "OrderItems" is an array of objects. so you can’t use OrderItems.OrderItemsOptions you will need to specify which element in the array. 
OrderItems.eindex].OrderItemsOptions
 

 

 


You’ve lost me… I am not proficient in javascript, any chance of highlighting where it should be inserted?

Thanks


Sure! OrderItems is an array of objects (As is OrderItemOptions). In this instance, OrderItems just contains 1 object (I am not sure if that will always be the case). To access items in an Array, you need to specify the index of the item you want to access. 0 being the first item, 1 being the second, etc… 

I.e.    var aOrderItemOptions = hBody.Body.Order.OrderItemss0].OrderItemOptions; 

would access the first item in OrderItems. If there were multiple order Items, you would need to set up a loop to access each Order Item’s Order Item Options.


I’ve highlighted the below indicating the orderItems array.

"OrderItems": r{
                "OrderItemOptions": {
                    "Metadata": {},
                    "MenuItemOptionPublicId": "98151ce3-XXXXXX-7bdc2f2b0cde",
                    "MenuItemOptionId": XXXXXX,
                    "IsMasterOptionSetItem": false,
                    "Name": "Can coke",
                    "Price": 1.5,
                    "MenuItemOptionDisplayOrder": 0,
                    "MenuItemOptionSetDisplayOrder": 0
                }],
                "Metadata": {},
                "MenuItemPublicId": "d263cea1-XXXXXXXXXXX4482674abef3",
                "MenuSectionName": "Household",
                "MenuSectionDisplayOrder": 0,
                "Name": "Firelog",
                "Description": "4 FOR 5 €",
                "Price": 1.99,
                "PriceIncludingOptionSetItems": 3.49,
                "MenuItemId": 2277055,
                "MenuItemDisplayOrder": 0,
                "IsAvailable": true
            }],


Oh man, today is not my day. 

 

var hBody = JSON.parse(inputData.sBody);
var hOrder = hBody.Body.Order;
var aOrderItems = hBody.Body.Order.OrderItems;
var aOrderItemOptions = hBody.Body.Order.OrderItemsm0].OrderItemOptions;

output = =];
for (const hOrderItem of aOrderItems){
output.push({"Product name": hOrderItem.Name,
"Price": hOrderItem.Price,
"Description Parent": hOrderItem.Description,
"Order date": hOrder.AcceptedFor,
"Order Item Options": OrderItems.OrderItemsOptions.Name});
}

 

Still getting “ ReferenceError: OrderItems is not defined “


Could that be from the last line where you reference 

"Order Item Options": OrderItems.OrderItemsOptions.Name} ? OrderItems has not been defined anywhere