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