Search code examples
javascriptjsonmongodbjsonpathparseint

parseInt() in script returning NaN because jsonpath syntax seems to not be read correctly


Using jsonpath dot notation, the key I need is at-

ProductDetails.Prices.BillingDivisor

Sample JSON document shown below...

{
    "UniqueID": "50962",
    "InvNum": "17001107",
    "Location": "017",
    "InvoiceDate": "6/19/2017 12:00:00 AM",
    "LINEITEM": "1",
    "CUSTID": "011022",
    "DEPTID": "004305",
    "PRODID": "070023",
    "QUANT": "0",
    "Unitprice": 1.887,
    "BILLUNIT": "GL",
    "QuantShare": 2000,
    "Tax": "False",
    "TaxExempt": "",
    "PriceType": "List",
    "DumItem": "False",
    "SourceNumber": "0",
    "SourceLocation": "",
    "SourceDate": "1/1/1900 12:00:00 AM",
    "LastUpdated": "2018-05-03T18:49:00-0400",
    "BookGuid": null,
    "ProductDetails": {
        "UniqueKey": 98269,
        "DepartmentID": "004305",
        "ProductID": "070023",
        "Accounts": {
            "DepartmentID": "004305",
            "ProductID": "070023",
            "SalesTaxable": false,
            "SalesAccount": "",
            "BeginningYearBalance": 0,
            "EndOfMonth1": 0,
            "EndOfMonth2": 0,
            "EndOfMonth3": 0,
            "EndOfMonth4": 0,
            "EndOfMonth5": 0,
            "EndOfMonth6": 0,
            "EndOfMonth7": 0,
            "EndOfMonth8": 0,
            "EndOfMonth9": 0,
            "EndOfMonth10": 0,
            "EndOfMonth11": 0,
            "EndOfMonth12": 0,
            "ProductStateTaxable": false,
            "PurchaseAccount": "",
            "FreightAccount": "",
            "PurchaseDiscount": ""
        },
        "Prices": {
            "DepartmentID": "004305",
            "ProductID": "070023",
            "BillingUnits": "GL",
            "BillingDivisor": "1",
            "AverageCost": "2.129",
            "List": "0",
            "LastPurchaseCost": "2.129"
        },
        "Classifications": {
            "Name-0": "5314: Dieselex",
            "Name-1": "B11 DYED:  B11 DYED",
            "Name-2": "Gallon Products",
            "Name-3": "FUEL-CvilleDYED",
            "Name-4": "Energy Comm"
        },
        "Attributes": {
            "Value-0": "",
            "Name-0": "Bag Weight",
            "Required-0": false,
            "MaximumLength-0": 0,
            "DisallowDuplicates-0": false,
            "Value-1": "",
            "Name-1": "GTIN",
            "Required-1": false,
            "MaximumLength-1": 0,
            "DisallowDuplicates-1": false
        },
        "LastUpdated": "2018-05-03T18:19:18-0400",
        "ProductClassification": "5314: Dieselex"
    }
}

Using the following javascript, all the values return properly except for this part:

parseInt(data.ProductDetails.Prices.BillingDivisor)

as I keep getting the result as NaN instead of 1.

var bulk = db.bookingsMaster.initializeOrderedBulkOp();
var counter = 0;
db.bookingsMaster.find().forEach(function (data) {
    var updoc = {
        "$set": {
        }
    };
    var unitPrice = "Unitprice";
    updoc[ "$set"][unitPrice] = parseFloat(data.Unitprice);

    var quantShare = "QuantShare";
    updoc[ "$set"][quantShare] = parseFloat(data.QuantShare);

    var billingDivisor = "ProductDetails.Prices.BillingDivisor";
    updoc[ "$set"][billingDivisor] = parseInt(data.ProductDetails.Prices.BillingDivisor);

    // queue the update
    bulk.find({
        "_id": data._id
    }).update(updoc);
    counter++;
    // Drain and re-initialize every 1000 update statements
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = db.bookingsMaster.initializeOrderedBulkOp();
    }
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();

Any assistance is greatly appreciated.


Solution

  • Your problem is that, unlike Unitprice and QuantShare, BillingDivisor is actually a string with double-quotes around the 1. If you try

    console.log(parseInt('"1"'))
    

    you will get NaN, as you are seeing.

    You need to strip the double-quotes off the value e.g.

    console.log(parseInt('"1"'.replace('"','')))
    

    which returns 1, as desired. So in your code, you need

    parseInt(data.ProductDetails.Prices.BillingDivisor.replace('"',''))