Generating Files From the one single json file using NodeJS -

Answer:

We have a one array of json file which import data from ‘./yourfile.js’

yourfile.js look like:

const data = [
  {
    SNo: 1,
    Category: 'System',
    CommandName: 'ADB Sideload',
    Title: 'ADB SideLoad - Android ADB Shell Commands',
    Meta:
      'Know What is ADB Sideload command and how to use the ADB Sideload command',
    H1: 'ADB Sideload',
    FAQ: 'What is ADB Sideload in android\n\n2.',
    Answer:
      'You can use the ADB Sideload commands to put the custom ROMs, install Google Apps packages, and also used for updating the current version for android',
    Answer2: 'To Be Done',
    CommandExampleTitle: 'Unzip a zip file',
    CommandExampleExamples: 'adb sideload <filename.Zip>',
    CommandMainDescription:
      'It is used to install custom ROM and unzip zip files',
    CommandOptions: 'None',
    CommandDetails: 'None',
  },
  {},
  {} ///add more object accordint to your json file
]
export default data

First, we are reading json data by iterating json object one by one. and stored json object value in single variable which you want.

after that. we created an object dataToGenerate{} inside object we put all the variables which we did stored in variables.

ensure that, filename extension should be .json, as we are giving in the code

index.js look like:

import fs from 'fs'
import data from './yourfile.js'

const disp = () => {
  for (let i = 0; i < data.length; i++) {
    let Category = data[i].Category
    let CommandName = data[i].CommandName
    let Title = data[i].Title
    let Meta = data[i].Meta
    let H1 = data[i].H1
    let FAQ = data[i].FAQ
    let Answer = data[i].Answer
    let Answer2 = data[i].Answer2
    let CommandExampleTitle = data[i].CommandExampleTitle
    let CommandExampleExamples = data[i].CommandExampleExamples
    let CommandMainDescription = data[i].CommandMainDescription
    let CommandOptions = data[i].CommandOptions
    let CommandDetails = data[i].CommandDetails

    let CommandNameLowerCase = CommandName.toLowerCase().split(' ').join('-')

    const dataToGenerate = {
      Category: `${Category}`,
      CommandName: `${CommandName}`,
      Title: `${Title}`,
      Meta: `${Meta}`,
      H1: `${H1}`,
      CommandMainDescription: `${CommandMainDescription}`,
      CommandOptions: [
        {
          Title: `${CommandExampleTitle}`,
          OptionDetail: `${CommandDetails}`,
          Example: `${CommandExampleExamples}`,
        },
      ],
      CommandExamples: [
        {
          Title: `${CommandExampleTitle}`,
          Example: `${CommandExampleExamples}`,
        },
      ],
      CommandotherDescription: `${CommandOptions}`,
      FAQS: [{ FAQ: `${FAQ}`, Answer: `${Answer}`, Answer2: `${Answer2}` }],
      CommandBlogLinks: [
        {
          LinkTitle: 'LinkTitle',
          Image: 'image.png',
          LinkURL: 'https://www.google.com',
        },
      ],
    }

    const htmlfile = `${CommandNameLowerCase}` + '.json'

    fs.writeFile(htmlfile, JSON.stringify(dataToGenerate), (err) => {
      if (err) {
        console.log(err)
      }
    })
  }
}

disp()

Finally, Last step run script:

node index.js

after this command multiple json file will be generated from the one single json file.

Enjoy the source code, If you thing this post useful for you. You can bookmark our website in your browser, just because we post daily cool blogpost related to every codding problem solutions with free (Source Code).