1

The json data is in the below pattern. And the Json data is coming from backend and getting through api and storing in a state variable.

{ "message": "user created successfully", "status": "success", "student": { "class": "10", "email": "[email protected]", "name": "User", "password": "user", "phone_number": "some phone number", "school": "1", "section": "a" } } 

I have stored the data which is returned through api in a state variable.

constructor(){ super(); this.state = { jsonData: '' } } 

And tried accessing using below fashion.

this.state.jsonData.status 

but not able to access. How can I access the status value in react?

4
  • can you show how are you calling the api? if you are calling your api in componentDidMount and in the response you need to set the state value as this.setState({jsonData: result}) like this Commented Apr 3, 2019 at 10:13
  • yeah I have did the same. And the jsonData variable contains the data in the same format which is shown above. @AnkushRishi Commented Apr 3, 2019 at 10:14
  • What does it mean "not able to access"? Are you getting an error? Commented Apr 3, 2019 at 10:17
  • can you update your post and show the code how did you set the state so that I can figure out the problem easily Commented Apr 3, 2019 at 10:25

1 Answer 1

1

Please check type of jsonData in state when you call it using typeof or instanceof.

It maybe by you store fetched data in string type without check and manipulating.

If it is string type, convert it using JSON.parse

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.