I am working with a json file that contains content for various pages categorized under "service". In my nextJS project, I utilize dynamic routes with a file named "[serviceId].tsx" for routing. This setup is functioning correctly. However, I am facing an issue where I need to access information from the json file using the [serviceId] provided in the route.
Here is the code snippet from my [serviceId].tsx file:
const json = jsonFile.services
const router = useRouter()
const serviceId = router.query.serviceId
return (
<div>
<ArticleWithPicture title={content.title} description={content.description}/>
</div>
)
}
My json file has a structure similar to this (edited for clarity in this example):
{
"serviceId":
[
{
"service1": {
"id": "xx",
"title": "xxx",
"description": "xx",
"featuredCompany":
[
{ "id": "1",
"name": "xxx",
"companyPageURL": "/",
"imagePath": "xxx",
"description": "xxx",
"additionalServices": {
"service1": "xxx",
"service2": "xxx"
},
"instagramURL":"/",
"twitterURL": "/"
}
]
}
},
{
"service2": {
"id": "xxx",
"title": "xxx",
"description": "xxx",
"featuredCompany":
[
{ "id": "1",
"name": "xxx",
"companyPageURL": "/",
"imagePath": "xxx",
"description": "xxx",
"additionalServices": {
"service1": "xxx",
"service2": "xx"
},
"instagramURL":"/",
"twitterURL": "/"
}
]
}
}
]
}
Each Service in the json file corresponds to individual pages. My objective is to dynamically set the title of the "ArticleWithPicture" component based on the title in the json file that matches the serviceId obtained from "router.query.serviceId". However, the code snippet below results in an error:
<ArticleWithPicture title={json.{serviceId}.title}/>
This issue is due to nesting curly braces. Is there a better way to approach this?
Directly accessing the title like this also does not work:
const title = json.serviceId.title
My actual goal is to query the json file based on the serviceId provided by "router.query.serviceId" like this:
const title = json.{serviceId}.title
It seems like there could be a problem either with my json file structure or my method of accessing it. Any guidance on this matter would be greatly appreciated.
Thank you!