1
\$\begingroup\$

The nested JSON file contains names of US states and corresponding cities. City.json

[
    {
      "header": "Michigan ",
      "id": "MI",
      "PlaceList": [
        {
          "title": "Detroit",
          "slug": "detroit"
        },
        {
          "title": "Lansing ",
          "slug": "lansing"
        }
      ]
    },
    {
      "header": "Iowa ",
      "id": "IA",
      "PlaceList": [
        {
          "title": "Ames",
          "slug": "ames"
        },
        {
          "title": "Davenport ",
          "slug": "davenport"
        }
      ]
    }
  ]

The following code returns details of the cities stored in markdown file.

import React, { useMemo } from 'react'
import fs from "fs"
import Link from "next/link"
import Markdown from "react-markdown"
import rehypeRaw from "rehype-raw"
import matter from "gray-matter"
import City from '@/app/(posts)/Blog/City.json'
import path from "path"
import { Metadata, ResolvingMetadata } from "next"

const allPosts = City.flatMap(({ header, id, PlaceList }) =>
contentList.map((topics) => ({

    stateName: header,
    stateId: id,
    ...topics,

  })),
);

// generates static pages 
export async function generateStaticParams() {
  return allPosts.map((post) => ({
    chapterID: post.stateId,
    postID: post.slug

  }))
}

interface Post {
  stateId: string;
  title: string;
  slug: string;
}

interface Pagination {
  prev: string | null;
  next: string | null;
  post: Post | null;
  switchState: boolean;
}

const BlogPage = ({params}:{params:any) => {
  //gets post ids from matching slug
  const postID = params.postID;
  // calculate prev/next page slug and switch state
  const { prev, next, post, switchState } = useMemo<Pagination>(() => {
    const postIndex = allPosts.findIndex(({ slug }) => slug === postID);

    if (postIndex === -1) {
    return {
      prev: null,
      next: null,
      post: null,
      switchState: false
    };
  }

    const currentPost = allPosts[postIndex];
    const isLastPostInHeader =
      postIndex === allPosts.length - 1 ||
      allPosts[postIndex + 1]?.stateId !== currentPost.stateId;

    return {
       prev: allPosts[postIndex - 1]?.slug || null,
       post: currentPost,
       next: isLastPostInState ? null : allPosts[postIndex + 1]?.slug || null,
       switchState: isLastPostInState
  };
  }, [postID]);


  if(!post) {
    return null;
  }
  const curr_post = GetPostContent(`${post.stateId}`,`${post.slug}`)
  return (

    <>

      <h1>{curr_post.title}</h1>
      <hr className="my-6 border border-dashed bg-sky-600 h-0.5" />
      <article lang="bn" className="prose lg:prose-xl">
        <Markdown rehypePlugins={[rehypeRaw]}>
          {curr_post.content}</Markdown>
      
      </article>
     <Link
        className="float-left text-teal-600"
        aria-disabled={!prev}
        href={prev ? `${prev}` : ''}
      >
        prev
      </Link>
      <Link
        className="float-right text-teal-600"
        aria-disabled={!next}
        href={next ? `${next}` : ''}
      >
        next
      </Link>
      
      )}
    </>

  )
}

export default BlogPage

Is there a better way to parse data from JSON in NextJS?

\$\endgroup\$
3
  • \$\begingroup\$ Unfortunately this question is off-topic because this site is for reviewing working code. Please take the tour and read up at our help center. When the code works then feel free to edit the post to include it for a review. \$\endgroup\$ Jan 12 at 21:32
  • \$\begingroup\$ I have updated the code, it seems to be working. Also updated the question. \$\endgroup\$
    – aries0152
    Jan 12 at 21:41
  • \$\begingroup\$ Okay - thank you. I've reopened the question \$\endgroup\$ Jan 12 at 22:01

0

Your Answer

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

Browse other questions tagged or ask your own question.