shahin
Active Member
I've written some code in vba to parse a price from json response. Although my code is successfully parsing the price, I'm having a hard time understanding an inverted comma related confusion. Hope somebody here would stretch a helping hand to clear the confusion.
This is the script:
This is the total response you can see if you follow the link used within the above script:
What I can't understand is:
I knew so far that if there is any name wrapped within inverted comma in the json file like "shahin", then it will be used like """shahin""" in the responsetext so that it can be split properly. Which means to diminish the effect of "" there should be used """". If I'm right and try to understand how the below expression worked:
Split(Split(Split(res, """displayPrice""")(1), """formattedValue"" : """)(1), """")(0)
1. """displayPrice""" ---there is nothing to complain according to how it looks in the json response.
2. """formattedValue"" : """ ---something to complain according to how it looks in the json response
Because the right one should be """formattedValue"""" : """ if the way i thought is right. However, it throws error. Where i'm going wrong? Thanks.
This is the script:
Code:
Sub Get_price()
Dim res As Variant
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.homedepot.ca/homedepotcacommercewebservices/v2/homedepotca/products/1000660019/localized/9999?catalogVersion=Online&lang=en", False
.send
res = .responseText
End With
res = Split(Split(Split(res, """displayPrice""")(1), """formattedValue"" : """)(1), """")(0)
MsgBox res
End Sub
This is the total response you can see if you follow the link used within the above script:
Code:
{
"bopis" : true,
"buyable" : false,
"onlineStock" : {
"stockLevel" : 0,
"stockLevelStatus" : "NoLongerAvailable"
},
"optimizedPrice" : {
"availabilityMsg" : "<a href=\"#\" data-target=\"#myStoreModal\" data-toggle=\"modal\">Select store for availability</a>",
"displayPrice" : {
"currencyIso" : "CAD",
"formattedValue" : "$11.29",
"priceType" : "BUY",
"value" : 11.29
},
"endDate" : "2017-11-03T23:59:59-04:00",
"productId" : "1000660019",
"productStatus" : "NA",
"regprice" : {
"currencyIso" : "CAD",
"formattedValue" : "$11.29",
"priceType" : "BUY",
"value" : 11.29
},
"savingsAmount" : {
"currencyIso" : "CAD",
"formattedValue" : "$0.00",
"priceType" : "BUY",
"value" : 0.0
}
},
"promotionMessages" : {
},
"shipToHome" : false
}
What I can't understand is:
I knew so far that if there is any name wrapped within inverted comma in the json file like "shahin", then it will be used like """shahin""" in the responsetext so that it can be split properly. Which means to diminish the effect of "" there should be used """". If I'm right and try to understand how the below expression worked:
Split(Split(Split(res, """displayPrice""")(1), """formattedValue"" : """)(1), """")(0)
1. """displayPrice""" ---there is nothing to complain according to how it looks in the json response.
2. """formattedValue"" : """ ---something to complain according to how it looks in the json response
Because the right one should be """formattedValue"""" : """ if the way i thought is right. However, it throws error. Where i'm going wrong? Thanks.