0

I want to import json_output_web the value of "p" from the given array in a cell of google sheets. I have never used JS before and I want to get the live price of the respective crypto pair. I used the following code in the Google apps script and received

function myFunction() {

var a = "https://public.coindcx.com/market_data/trade_history?pair=I-BTC_INR&limit=1";
var response = UrlFetchApp.fetch(a);
var jsonresponse = JSON.parse(response);

Logger.log(response)
Logger.log(jsonresponse)

}
JSon_initial_output. Further, I made changes in the code to line 9 in order to obtain the value of p but the logger shows null. `function myFunction() {
var a = "https://public.coindcx.com/market_data/trade_history?pair=I-BTC_INR&limit=1";
var response = UrlFetchApp.fetch(a);
var jsonresponse = JSON.parse(response);

Logger.log(response)
Logger.log(jsonresponse)
Logger.log(jsonresponse.p)

}`

Where am I mistaken?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Please post plain text instead of links to screenshots. – Barmar Mar 26 '21 at 17:31
  • Don't you see that the object is inside an array? `jsonresponse[0].p` – Barmar Mar 26 '21 at 17:31
  • It is an object, which makes it very easy to access as Barmar showed. For a bit more background since you said you are new at this, check out the tutorial on W3Schools: https://www.w3schools.com/js/js_objects.asp – Kris Mar 26 '21 at 19:12
  • @Barmar Yeah it was quite literally my first question so I was unaware with the common practices. I'll keep that in mind. I'm sorry I didn't quite understand your 2nd comment. I did research a few websites about the type of object it was and I found out it was array as I mentioned in the question topic. I don't understand anything about JS, so I couldn't get you at first. BTW I thank you a ton. You just solved one of the most craving question for me. I cannot thank you enough for it. – Anshuman Sharma Apr 04 '21 at 17:21
  • @KrispinMiller Thank you too for helping out with the guide!!!!!!!! – Anshuman Sharma Apr 04 '21 at 17:22
  • But why do we place 0 while querying the object instead of "p" as mentioned in the guide by @KrispinMiller? Placing that returns a null value output – Anshuman Sharma Apr 04 '21 at 17:28
  • The json you provided opens with [{.... The items inside the aquare bracket are an array, the items inside the curly bracket are an object. To access an element inside an array you use Variable[index]. In this case, since it is the first element in the array, it has an index value of 0. So Logger.log[0].p gives you the property p in the first object of the parent array. – Kris Apr 04 '21 at 18:28
  • Perfect. Thank you! – Anshuman Sharma Apr 05 '21 at 19:07
  • If you just need the current price for any given coin, please consider doing `=IMPORTDATA("https://cryptoprices.cc/BTC/")`. It avoids the need to do any parsing and save a lot of time. – Pedro Jun 28 '21 at 10:52

0 Answers0