1

I am trying to loop through the below JSON data, extract the total_price field, sum it, then add the amount field, found in refunds[<i>].transactions array. As mentioned below I'm able to grab the total_price fields and sum, however am lost on how to also grab and sum the refund amount fields, which will be used to subtract from the total_price sum.

JSON:

{
"sales_mtd": {
"orders": [
{
"created_at": "2021-06-25T16:30:47-04:00",
"fulfillment_status": null,
"name": "#1099",
"refunds": [
{
"admin_graphql_api_id": "gid://shopify/Refund/800483278870",
"created_at": "2021-06-25T19:18:05-04:00",
"duties": [],
"id": 800483278870,
"note": "",
"order_adjustments": [],
"order_id": 4071975550998,
"processed_at": "2021-06-25T19:18:05-04:00",
"refund_line_items": [],
"restock": true,
"total_duties_set": {
"presentment_money": {
"amount": "0.00",
"currency_code": "CAD"
},
"shop_money": {
"amount": "0.00",
"currency_code": "CAD"
}
},
"transactions": [
{
"admin_graphql_api_id": "gid://shopify/OrderTransaction/5016768315414",
"amount": "75.71",
"authorization": null,
"created_at": "2021-06-25T19:18:05-04:00",
"currency": "CAD",
"device_id": null,
"error_code": null,
"gateway": "manual",
"id": 5016768315414,
"kind": "refund",
"location_id": null,
"message": "Refunded 75.71 from manual gateway",
"order_id": 4071975550998,
"parent_id": 5015934074902,
"processed_at": "2021-06-25T19:18:05-04:00",
"receipt": {},
"source_name": "1830279",
"status": "success",
"test": false,
"user_id": 64086245398
}
],
"user_id": 64086245398
}
],
"total_price": "75.71"
},

I used this code to loop through the total_price fields and sum them, thanks to help in another question.

let sumReduce = json.sales_mtd.orders.reduce((acc, curr) => acc + Number(curr.total_price), 0);
console.log(sumReduce);

However, when I try and repurpose this to also grab and sum the refund amounts, I either get NaN or an error message unable to read the transaction or other field. I have tried reviewing MDN docs, starting from a less complex object, repurposing other answers but can't quite seem to get it. Any input is very much appreciated.

Thank you

6
  • 1
    Could you post the code you tried that gave you this error? In the code you posted here I don't see any transaction field in the json object Commented Jun 26, 2021 at 20:11
  • @Snirka sorry wrong json, added the correct one Commented Jun 26, 2021 at 20:20
  • What field are you trying to sum? There is no transaction field you have transactions, balance_transaction, network_transaction_id and more. Commented Jun 26, 2021 at 20:43
  • Trying to sum the "amount" field, found under "transactions", which is part of the refunds array Commented Jun 26, 2021 at 20:53
  • Did the below answer solved your problem? Commented Jun 27, 2021 at 9:54

2 Answers 2

1

Try the following:

let sumReduce1 =  json.sales_mtd.orders.reduce((acc, order) => {
  if(order.refunds.length !== 0) {
    return acc + Number(order.refunds[0].transactions[0].amount);
  } else {
    return acc;
  }
}, 0);

let sumReduce2 =  json.sales_mtd.orders.reduce((acc, order) => {
  if(order.refunds.length !== 0) {
    return order.refunds.reduce((acc1, refund) => {
      return refund.transactions.reduce((acc2, transaction) => acc2 + Number(transaction.amount), acc1);
    }, acc);
  } else {
    return acc;
  }
}, 0);

sumReduce1 and sumReduce2 do the same thing for the JSON object you provide only that sumReduce2 take into consideration that you can have multiple orders, refunds and transactions. sumReduce1 is more of a hard coded function, I would advise using sumReduce2.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like to me "transactions" is only available within the refunds array, and sometimes it is empty.

something like if(refunds.length) will do the job

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.