从响应中提取值,并将其用于下一个特定的采样器请求主体

问题描述

如何从响应正文中提取510580051058015105802之类的ID及其数量2005040如下所示在请求正文中重复使用它?

响应正文:

{
    "data": {
        "callOrders": [
            {
                "promotionId": null,"Promotion": null,"Lines": [
                    {
                        "id": 5105800,"quantity": 200,"skuId": 85,"callId": 494281,"skuBatchId": 587,"amountDetails": {
                            "rate": 144.22,"grossAmount": 32593.72,"netAmount": 32593.72,"taxAmount": 3749.72,"taxableAmount": 28844,"subTotal": 28844,"billdiscount": 0,"Tradediscount": 0,"discountAmount": 0,"promotiondiscount": 0,"topUpdiscount": 0,"__typename": "AmountDetail"
                        },"rateDetails": {
                            "rlp": 144.22,"rlpWithVat": 162.9686,"netPrice": 144.22,"netPriceWithVat": 162.9686,"__typename": "RateDetail"
                        },"SKU": {
                            "id": 85,"title": "Ariel Oxyblu 500 gm x 48 [82262305]","__typename": "SKU"
                        },"SKUBatch": {
                            "priceDetails": {
                                "rlp": 144.22,"dlp": 137.35,"vatPercentage": 0.13,"mrpSrp": 176,"mrpStatus": true,"__typename": "SKUPrice"
                            },"batchDetails": {
                                "batchNumber": "DEFAULT_BATCH","__typename": "SKUBatch"
                            },"usageDate": {
                                "manufacture": "0000-00-00","expiry": "0000-00-00","__typename": "SKUUsage"
                            },"updatedAt": "2018-12-01","active": true,"__typename": "SKUBatchRate"
                        },"Promotion": {
                            "id": null,"title": null,"type": null,"scope": null,"criteria": null,"__typename": "Promotion"
                        },"promotionId": null,"distributorId": 16,"__typename": "Line","inStock": "INSTOCK","freeSku": false,"focusedSku": true
                    },{
                        "id": 5105801,"quantity": 50,"skuId": 82,"skuBatchId": 551,"amountDetails": {
                            "rate": 281.88,"grossAmount": 15926.22,"netAmount": 15926.22,"taxAmount": 1832.22,"taxableAmount": 14094,"subTotal": 14094,"rateDetails": {
                            "rlp": 281.88,"rlpWithVat": 318.5244,"netPrice": 281.88,"netPriceWithVat": 318.5244,"SKU": {
                            "id": 82,"title": "Ariel Oxyblu 1 kg x 24 [82250306]","SKUBatch": {
                            "priceDetails": {
                                "rlp": 281.88,"dlp": 268.45,"mrpSrp": 344,"updatedAt": "2018-01-31",{
                        "id": 5105802,"quantity": 40,"skuId": 769,"skuBatchId": 733,"amountDetails": {
                            "rate": 197.53,"grossAmount": 8928.356,"netAmount": 8928.356,"taxAmount": 1027.156,"taxableAmount": 7901.2,"subTotal": 7901.2,"rateDetails": {
                            "rlp": 197.53,"rlpWithVat": 223.2089,"netPrice": 197.53,"netPriceWithVat": 223.2089,"SKU": {
                            "id": 769,"title": "H&S 2in1 Active Protect 180 ml x 24 [82302894]","SKUBatch": {
                            "priceDetails": {
                                "rlp": 197.53,"dlp": 186.35,"mrpSrp": 250,"updatedAt": "2019-11-05","focusedSku": false
                    }
                ],"__typename": "Promotionorder"
            }
        ]
    }
}

请求正文:

{
    "operationName": null,"variables": {
        "input": {
            "invoiceId": 3683663,"orderDetails": [
                {
                    "id": 5105800,"quantity": 200
                },{
                    "id": 5105801,"quantity": 50
                },{
                    "id": 5105802,"quantity": 40
                }
            ]
        },"type": "CatalogDetail"
    },"query": "mutation ($input: dispatchInput!) {\n dispatchInvoice(input: $input) {\n id\n __typename\n }\n}\n"
}

解决方法

使用JavaScript时,您可以使用以下一种:)

const responseBody = {
    "data": {
        "callOrders": [
            {
                "promotionId": null,"Promotion": null,"Lines": [
                    {
                        "id": 5105800,"quantity": 200,"skuId": 85,"callId": 494281,"skuBatchId": 587,"amountDetails": {
                            "rate": 144.22,"grossAmount": 32593.72,"netAmount": 32593.72,"taxAmount": 3749.72,"taxableAmount": 28844,"subTotal": 28844,"billDiscount": 0,"tradeDiscount": 0,"discountAmount": 0,"promotionDiscount": 0,"topUpDiscount": 0,"__typename": "AmountDetail"
                        },"rateDetails": {
                            "rlp": 144.22,"rlpWithVat": 162.9686,"netPrice": 144.22,"netPriceWithVat": 162.9686,"__typename": "RateDetail"
                        },"SKU": {
                            "id": 85,"title": "Ariel Oxyblu 500 gm x 48 [82262305]","__typename": "SKU"
                        },"SKUBatch": {
                            "priceDetails": {
                                "rlp": 144.22,"dlp": 137.35,"vatPercentage": 0.13,"mrpSrp": 176,"mrpStatus": true,"__typename": "SKUPrice"
                            },"batchDetails": {
                                "batchNumber": "DEFAULT_BATCH","__typename": "SKUBatch"
                            },"usageDate": {
                                "manufacture": "0000-00-00","expiry": "0000-00-00","__typename": "SKUUsage"
                            },"updatedAt": "2018-12-01","active": true,"__typename": "SKUBatchRate"
                        },"Promotion": {
                            "id": null,"title": null,"type": null,"scope": null,"criteria": null,"__typename": "Promotion"
                        },"promotionId": null,"distributorId": 16,"__typename": "Line","inStock": "INSTOCK","freeSku": false,"focusedSku": true
                    },{
                        "id": 5105801,"quantity": 50,"skuId": 82,"skuBatchId": 551,"amountDetails": {
                            "rate": 281.88,"grossAmount": 15926.22,"netAmount": 15926.22,"taxAmount": 1832.22,"taxableAmount": 14094,"subTotal": 14094,"rateDetails": {
                            "rlp": 281.88,"rlpWithVat": 318.5244,"netPrice": 281.88,"netPriceWithVat": 318.5244,"SKU": {
                            "id": 82,"title": "Ariel Oxyblu 1 kg x 24 [82250306]","SKUBatch": {
                            "priceDetails": {
                                "rlp": 281.88,"dlp": 268.45,"mrpSrp": 344,"updatedAt": "2018-01-31",{
                        "id": 5105802,"quantity": 40,"skuId": 769,"skuBatchId": 733,"amountDetails": {
                            "rate": 197.53,"grossAmount": 8928.356,"netAmount": 8928.356,"taxAmount": 1027.156,"taxableAmount": 7901.2,"subTotal": 7901.2,"rateDetails": {
                            "rlp": 197.53,"rlpWithVat": 223.2089,"netPrice": 197.53,"netPriceWithVat": 223.2089,"SKU": {
                            "id": 769,"title": "H&S 2in1 Active Protect 180 ml x 24 [82302894]","SKUBatch": {
                            "priceDetails": {
                                "rlp": 197.53,"dlp": 186.35,"mrpSrp": 250,"updatedAt": "2019-11-05","focusedSku": false
                    }
                ],"__typename": "PromotionOrder"
            }
        ]
    }
};

const orderDetails = [];
responseBody.data.callOrders.forEach((entry) => {
    entry.Lines.forEach(({ id,quantity }) => {
        orderDetails.push({ id,quantity });
    })
});

const requestBody = {
    "operationName": null,"variables": {
        "input": {
            "invoiceId": 3683663,"orderDetails": orderDetails
        },"type": "CatalogDetail"
    },"query": "mutation ($input: DispatchInput!) {\n dispatchInvoice(input: $input) {\n id\n __typename\n }\n}\n"
};

console.log(requestBody);