How to get deeper in the JSON body of a chainlink req.add("path") request? Add 2+ paths

Viewed 346

I can get the results of the chainlink path with req.add("path", "chainlink")

However, I want to return the price value, of "chainlink", "USD". The output json has two paths, how do I reach the second path to get the price value?

  function requestLINKPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://api.coingecko.com/api/v3/simple/price?ids=chainlink&vs_currencies=usd");
    req.add("path", "chainlinkUSD");
    req.addInt("times", 100);
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }

Here is the JSON response of the API

{
  chainlink: {
    usd: 3.78
  }
}
1 Answers

You can use the copy adapter with the copyPath syntax.

string[] memory copyPath = new string[](2);
copyPath[0] = "chainlink";
copyPath[1] = "USD";
req.addStringArray("copyPath", copyPath);

Here is the whole function.

  function requestLINKPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://api.coingecko.com/api/v3/simple/price?ids=chainlink&vs_currencies=usd");
    string[] memory copyPath = new string[](2);
    copyPath[0] = "chainlink";
    copyPath[1] = "USD";
    req.addStringArray("copyPath", copyPath);
    req.addInt("times", 100);
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }
Related