How do I refer to this JSON Object Data?

Viewed 55

I want to reference my Strapi database to my Next.JS Frontend. How can I reference a JSON data if it is also an object within the object?

As an example, to access the name, I use {activity.name}. How can I access the providerName?

I tried {activity.provider.providerName} but it is not working. What am I doing wrong?

[
  {
    "id": 1,
    "name": "Bowling in Mundsburg",
    "activityType": "Bowling",
    "profileType": "Basic",
    "adventureType": "Activity",
    "slug": "bowling-in-new-york",
    "provider": {
      "id": 1,
      "providerName": "Bowlingcenter New York",
      "slug": "bowlingcenter-new-york",
      "published_at": "2021-09-26T12:06:12.670Z",
      "created_at": "2021-09-26T12:04:28.585Z",
      "updated_at": "2021-09-26T12:06:12.690Z"
    },
    ...
]
1 Answers

could you try like so?

const strapiRes = [
 {
    "id": 1,
    "name": "Bowling in Mundsburg",
    "activityType": "Bowling",
    "profileType": "Basic",
    "adventureType": "Activity",
    "slug": "bowling-in-new-york",
    "provider": {
      "id": 1,
      "providerName": "Bowlingcenter New York",
      "slug": "bowlingcenter-new-york",
      "published_at": "2021-09-26T12:06:12.670Z",
      "created_at": "2021-09-26T12:04:28.585Z",
      "updated_at": "2021-09-26T12:06:12.690Z"
    }
  }
];
const whatYouAreAskingFor = strapiRes[0].provider.providerName

also if you are looping try something like:

strapiRes.map(activity => activity.provider.providerName)
Related