{
    "id": 1234,
	"order_number": "TEST",
	"created_at": "2024-07-14T16:49:42-04:00",
	"currency": "USD",
	"total_discounts": "85.59",
	"user_id": 12345,
	"line_items": [
        {
        "id": 12818791301255,
        "quantity": "8",
        "sku": "CHEWDS60-MSM",
        "product_id": 7869000220807,
        "price": "106.99",
        "total_discount": "0.00",
        "discount_allocations": [
            {
                "amount": "85.59",
                "discount_application_index": 0
            }
        ]
        },
        {
        "id": 12818791301256,
        "quantity": "1",
        "sku": "CQOMSC60-MSM",
        "product_id": 7873207369863,
        "price": "24.99",
        "total_discount": "0.00",
        "discount_allocations": [
            {
                "amount": "0.00",
                "discount_application_index": 0
            }
        ]
        }
    ]
}




{
    "id": 1234,
	"order_number": "TEST",
	"created_at": "2024-07-14T16:49:42-04:00",
	"currency": "USD",
	"total_discounts": "85.59",
	"user_id": 12345,
	"line_items": [
        {
        "id": 12818791301255,
        "quantity": "8",
        "sku": "CHEWDS60-MSM",
        "product_id": 7869000220807,
        "price": "106.99",
        "total_discount": "85.59"
        },
        {
        "id": 12818791301256,
        "quantity": "1",
        "sku": "CQOMSC60-MSM",
        "product_id": 7873207369863,
        "price": "24.99",
        "total_discount": "0.00"
        }
    ]
}











const jsonString = `{
    "id": 1234,
    "order_number": "TEST",
    "created_at": "2024-07-14T16:49:42-04:00",
    "currency": "USD",
    "total_discounts": "85.59",
    "user_id": 12345,
    "line_items": [
        {
        "id": 12818791301255,
        "quantity": "8",
        "sku": "CHEWDS60-MSM",
        "product_id": 7869000220807,
        "price": "106.99",
        "total_discount": "0.00",
        "discount_allocations": [
            {
                "amount": "85.59",
                "discount_application_index": 0
            }
        ]
        },
        {
        "id": 12818791301256,
        "quantity": "1",
        "sku": "CQOMSC60-MSM",
        "product_id": 7873207369863,
        "price": "24.99",
        "total_discount": "0.00",
        "discount_allocations": [
            {
                "amount": "0.00",
                "discount_application_index": 0
            }
        ]
        }
    ]
}`;

let data = JSON.parse(jsonString);

// Iterate over each line item to update the total_discount
data.line_items.forEach(item => {
    if (item.discount_allocations && item.discount_allocations.length > 0) {
        // Sum up all the amounts in discount_allocations and assign it to total_discount
        const totalDiscount = item.discount_allocations.reduce((sum, allocation) => {
            return sum + parseFloat(allocation.amount);
        }, 0);
        item.total_discount = totalDiscount.toFixed(2);
    }
    // Optionally remove the discount_allocations array if no longer needed
    delete item.discount_allocations;
});

// Convert the modified object back to a JSON string
const modifiedJsonString = JSON.stringify(data, null, 4);

console.log(modifiedJsonString);





































































"line_items": [
{
    "id": 12818791301255,
    "admin_graphql_api_id": "gid://shopify/LineItem/12818791301255",
    "attributed_staffs": [],
    "fulfillable_quantity": 12,
    "fulfillment_service": "manual",
    "fulfillment_status": null,
    "gift_card": false,
    "grams": 0,
    "name": "Cosequin®ASU Balance pellets - referrer",
    "price": "106.99",
    "price_set": {
        "shop_money": {
            "amount": "106.99",
            "currency_code": "USD"
        },
        "presentment_money": {
            "amount": "106.99",
            "currency_code": "USD"
        }
    },
    "product_exists": true,
    "product_id": 7869000220807,
    "properties": [],
    "quantity": 12,
    "requires_shipping": true,
    "sku": "CHEWDS60-MSM",
    "taxable": false,
    "title": "Cosequin®ASU Balance pellets",
    "total_discount": "0.00",
    "total_discount_set": {
        "shop_money": {
            "amount": "0.00",
            "currency_code": "USD"
        },
        "presentment_money": {
            "amount": "0.00",
            "currency_code": "USD"
        }
    },
    "variant_id": 41844739244167,
    "variant_inventory_management": null,
    "variant_title": "referrer",
    "vendor": "cosequin-tst1",
    "tax_lines": [],
    "duties": [],
    "discount_allocations": [
        {
            "amount": "192.58",
            "amount_set": {
                "shop_money": {
                    "amount": "192.58",
                    "currency_code": "USD"
                },
                "presentment_money": {
                    "amount": "192.58",
                    "currency_code": "USD"
                }
            },
            "discount_application_index": 0
        }
    ]
}]


