How to change the format of object in Azure Function using Java Script

时间:2017-12-18 06:51:55

标签: javascript json node.js azure-functions

I have an azure function which is responsible to call "someStoreprocedure" and pass req as an object.

this same req get inserted into the azure collection

The data in req is as below

`{
    "intObjectName" : "JCI-AOMS-SWM",
    "aomsOrderReleaseNbr" : "7232046001",
    "shipToFaxNbr" : "7325609699",
    "50records" : [ {
        "aomsLineNbr" : 1,
        "planShipPtShipDate" : "20170101",
        "product" : {
            "name" : "test-product-train",
            "productDisplayName" : "test-product-train-display",
            "sku" : "TRAIN-SKU",
        },
        "licenses" : [ {
            "productKey" : "productKey-1",
            "status" : "not activated"
        }, {
            "productKey" : "productKey-2",
            "status" : "not activated"
        },

        {
            "productKey" : "productKey-3",
            "status" : "not activated"
        } ]
    } ],
    "isEntitlementInProgress" : true,
    "id" : "1dcf296e-4e2f-b3f5-8117-92a7879e0a9b"
}` 

I want to change it to a different format like below and only same need to send to store procedure for insertion.

`{
intObjectName: "JCI-AOMS-SWM"
productKeys: [
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
{
            "productKey" : "productKey-1",
            "status" : "not activated"
},
]
}` 

My JS code is below,

Please let me know for the correction in code.

var DocumentDBClient = require('documentdb').DocumentClient;
module.exports = function(context, req) {
    var host = "some";
    var masterKey = "some=";
    var spName = "someStoreprocedure";
    var client = "";
    client = new DocumentDBClient(host, {
        masterKey : masterKey
    });

    var insertSPLink = "dbs/" + "admin" + "/colls/" + "productsoutput"
            + "/sprocs/" + spName;
    client.executeStoredProcedure(insertSPLink,req,function(err, res) {
                        if (err) {
                            return callback(statusObj);
                        } else {
                            context.log("Success in Insertion");
                            context.done();
                            return context;
                        }
                    });
};

2 个答案:

答案 0 :(得分:0)

例如:

var req = {
    "intObjectName": "JCI-AOMS-SWM",
    "aomsOrderReleaseNbr" : "7232046001",
    "shipToFaxNbr" : "7325609699",
    "50records" : [ {
        "aomsLineNbr" : 1,
        "planShipPtShipDate" : "20170101",
        "product" : {
            "name" : "test-product-train",
            "productDisplayName" : "test-product-train-display",
            "sku" : "TRAIN-SKU",
        },
        "licenses" : [ {
            "productKey" : "productKey-1",
            "status" : "not activated"
        }, {
            "productKey" : "productKey-2",
            "status" : "not activated"
        },

        {
            "productKey" : "productKey-3",
            "status" : "not activated"
        } ]
    } ],
    "isEntitlementInProgress" : true,
    "id" : "1dcf296e-4e2f-b3f5-8117-92a7879e0a9b"
};

var formatedReq = {
    intObjectName: req["intObjectName"],
    productKeys: req["50records"][0]["licenses"]
}

console.log(formatedReq);

输出:

{
    intObjectName: 'JCI-AOMS-SWM',
    productKeys: [
        {
            productKey: 'productKey-1',
            status: 'not activated'
        },
        {
            productKey: 'productKey-2',
            status: 'not activated'
        },
        {
            productKey: 'productKey-3',
            status: 'not activated'
        }
    ]
}

答案 1 :(得分:0)

我找到了解决问题的方法:

以下是完整的代码: -

var DocumentDBClient = require('documentdb').DocumentClient;

var newRecordOutputObj;

module.exports = function(context, req) {

    context.log('In orderTiggerRecords');

    var host = "some";
    var masterKey = "some=";
    var spName = "insertRecords";
    var outputCollection = "records";
    var databaseName = "some";
    var client = "";
    client = new DocumentDBClient(host, {
        masterKey : masterKey
    });
    var storedProc = "dbs/" + databaseName + "/colls/" + outputCollection
            + "/sprocs/" + spName;

    newRecordOutputObj = req;
    context
            .log("====================Complete Order JSON==============================");
    context.log(JSON.stringify(newRecordOutputObj));

    var orderData = {};

    for (var i = 0; i < newRecordOutputObj[0]['50records'][0].licenses.length; i++) {
        orderData = orderData
                + {
                    productkey : newRecordOutputObj[0]['50records'][0].licenses[i].productKey,
                    status : newRecordOutputObj[0]['50records'][0].licenses[i].status

                };
    }

    context
            .log("==================== Inserting into Record Collection ====================");
    context.log(orderData);

    client
            .executeStoredProcedure(
                    storedProc,
                    orderData,
                    function(err, res) {
                        if (err) {
                            spName = "";
                            fetchSPLink = "";
                            return callback(statusObj);
                        } else {
                            context
                                    .log("=========================== Done ==============================");
                            context.res = {
                                status : 200,
                            };
                            context.done();
                            return context;
                        }
                    });
}