Projekat Pr
Projekat Pr

Reputation: 37

How to extract data from nested json in react

I have madethe following code. So I have to retrieve data from json and present them in react. I have basicly one nested .joson object inside the other one. I know to to retrieve needed data from "root" json, but have a problem with doing it for other one. In this example I got as output enter image description here Now I don't need all thath json object inside, just Neko from name. I have tried {props.pred.nastavnik.name} but it did not work. (it didn't gave any output)

 <CardContent>
            <Typography className={classes.title} color="textSecondary" gutterBottom>
                Nastavnik
            </Typography>
            <Typography variant="h5" component="h2">
                {props.pred.nastavnik}
            </Typography>
            <Typography className={classes.title} color="textSecondary" gutterBottom>
                Br studenata
            </Typography>
            <Typography variant="h5" component="h2">
                {props.pred.broj_studenata}
            </Typography>



        </CardContent>

Every help would be great

Upvotes: 0

Views: 278

Answers (1)

Ho&#224;ng Kim Minh
Ho&#224;ng Kim Minh

Reputation: 172

nastavnik variable is a string. You should convert it to object by: JSON.parse(str) For example:

const str = '{"id":2,"admin":false,"name":"Neko","sifra":"sifra"}'
console.log(JSON.parse(str))

Upvotes: 1

Related Questions