0

I'm trying to figure out how to make a function that takes an input then uses it to grab a specific JSON object.

My data is in rpcData but I want the input from the obj function parameter to expand on this.

For example obj = ".market_data.current_price.usd" which would then be appended to JSON.stringify(rpcData) making it JSON.stringify(rpcData.market_data.current_price.usd)

export const Blockchain = ({ path, obj }) => {
  const { rpcData, isLoading, isError } = fetchRPC(path);
  if (isLoading) return <Loading />;
  if (isError) return <Error />;
  return JSON.stringify(rpcData);
};

Using one of the answers from here I was able to get it to work up to one level deep with JSON.stringify(rpcData[obj]) when obj="height" but still can't get it to work with deeper nested objects.

  • 2
    Seems like you want something like `return JSON.stringify(rpcData.result[obj]);`. But your question is not asked very clearly. – Marc Nov 01 '20 at 06:14
  • I can't understand exactly the problem you're facing, can you please elaborate on it? What is the desired output? – VIshal Jain Nov 01 '20 at 06:14
  • @Marc This is it, thanks. I'm a beginner and also not a very good communicator lol. I'll try to make the question more clear for anyone that finds this. – Gordon Lenz Nov 01 '20 at 06:18

0 Answers0