Home > Blockchain >  How to access data inside Json nested objects from an API
How to access data inside Json nested objects from an API

Time:11-30

I am using cryptocomare API to get crypto coins data within a Nextjs App. What i doing is that when a user clicks on a perticular symbol, i redirect it to the coin details page where i try to extract the clicked symbol with getServerSideProps as follows and then dynamically put in the API call and send it to the API server.

`

export const getServerSideProps = async (context) => {
  const res = await fetch(
    `https://min-api.cryptocompare.com/data/pricemultifull?tsyms=USD&fsyms=${context.params.symbol}`
  );  
  const icon = await res.json();
  return {
    props: {
      icon,
    },
  };
};

` This call returns a json object of nested objects and it goes to 2-3 levels deep. On Top it looks like following: API call response

Inside my code, I want to access the data Object -> RAW -> (whatever the user clicked on). But, Since the Symbol or coin queried by the user is dynamic (means i can't predict what is clicked) I never know what to query. SO i tried this to access the data object.RAW[0]

In principal it should give me the whatever object is inside the object.RAW But it returns undefined

Can please someone guide me , how can i get the data inside object.RAW without knowing what is inside? Thanks!

I have tried object.RAW[0] to access the data...,....

CodePudding user response:

You can use Object.values(object.RAW) to get an array of the values inside RAW (assuming RAW is not undefined)

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

  • Related