0

SomeOne please help me. I am spending too long trying to display some data I wish to iterate over a nested object json response using react js and access ["1. open"] after the date. The image of json response

the json data is { "Meta Data": { "1. Information": "Weekly Prices (open, high, low, close) and Volumes", "2. Symbol": "IBM", "3. Last Refreshed": "2022-06-08", "4. Time Zone": "US/Eastern" }, "Weekly Time Series": { "2022-06-08": { "1. open": "142.9800", "2. high": "144.7300", "3. low": "140.1500", "4. close": "140.8300", "5. volume": "10659817" },

render() { console.log(this.state.users);

   return (
     // div>{this.state.users["Meta Data"]["2. Symbol"]}</div>
     <div> {Object.keys(this.state.users).map((item, i) => (
         <div key={i}> {this.state.users[item]["2022-06-08"]} 
              {Object.keys(["2022-06-08"]).map((c, i) => (
                <li key={i}>{["2022-06-08"][c]["1. open"]}</li>
              ))}
         </div>
          


       ))}


    </div> 
   );

But the error shows enter image description here

2
  • Can you please paste out your JSON here in the question instead of image? That will help to answer it. Commented Jun 9, 2022 at 10:24
  • { "Meta Data": { "1. Information": "Weekly Prices (open, high, low, close) and Volumes", "2. Symbol": "IBM", "3. Last Refreshed": "2022-06-08", "4. Time Zone": "US/Eastern" }, "Weekly Time Series": { "2022-06-08": { "1. open": "142.9800", "2. high": "144.7300", "3. low": "140.1500", "4. close": "140.8300", "5. volume": "10659817" }, Commented Jun 9, 2022 at 10:30

2 Answers 2

0

This will work for you. Please refer to the demo too I made for you.

Stackblitz Demo: https://stackblitz.com/edit/react-ts-7zuxq4?file=index.tsx,App.tsx

 <div>
      {Object.keys(weeklyTimeSeries).map((key) => (
        <ul>
          <b>{key}</b>

          {Object.keys(weeklyTimeSeries[key]).map((ckey) => (
            <li>
              {ckey} = {weeklyTimeSeries[key][ckey]}
            </li>
          ))}
        </ul>
      ))}
 </div>

Note: I have made a general demo. So you have to map through your state variable instead of mine.

Sign up to request clarification or add additional context in comments.

8 Comments

Hello Thank you for helping me. But I am getting error Uncaught Error: Objects are not valid as a React child (found: object with keys {1. open, 2. high, 3. low, 4. close, 5. volume}).
I think it is because the map is not able to get the innermost data of 1.open object
the api data is from this website: alphavantage.co/…
I think you just need to make one check over this code. So once your data available then only perform this operation.
{Object.keys(this.state.users).map((key) => ( <ul> <b>{key= "Weekly Time Series"}</b> {Object.keys(this.state.users[key]).map((ckey) => ( <li> {ckey} </li> ))} </ul> ))} I got one output
|
0

use recursive approach to loop through the json object you will get it.

6 Comments

I am not able to recursively loop through. I am creating syntax error i believe. And the link is converting object into arrays but the data I have is pretty long for object to arrray manual conversion
Add the json data sample if possible
alphavantage.co/… the data is from this website
I hope you found the solution on previous answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.