title: Writing an object to an existing json file and wrapping the contents with '[ ]'
body: <p>Ok, so I'm learning javascript (coming from c++) by writing a utility that takes in .md (markdown) files and parses/transforms the file to an html then to a json object, then writes that object to an existing json file in a format that follows my specification.</p>
  <p>to be clear see the following pipeline:</p>
  <p><strong>loop through a directory of .md files</strong> -&gt; <strong>push each file through a parse/transform phase that turns markdown to html</strong> -&gt; <strong>push the html and parsed data to a json file</strong></p>
  <p><em>please note that I have most of the logic completed, however, I need help with a formatting issue...please read the full post before closing or answering. Thank you!</em></p>
  <p>main logic of the pipeline:</p>
  <pre><code>export function transformMdtoHtmltoJson(dest, post) {
      
      const file = matter.read(post);
      
      const title = file.data.title;
      const date = file.data.date;
      const description = file.data.description;
      const socials = file.data.socials;
      
      unified()
          .use(remarkParse)
          .use(remarkRehype)
          .use(rehypeStringify)
          .process(file.content)
          .then(
              (result) =&gt; {
                  toJson(dest, title, date, description, result.value, socials);
              },
              (err) =&gt; {
                  throw err;
              }
            );
  }
  </code></pre>
  <p>My plugin to write the json to an existing file:</p>
  <pre><code>class Data {
  
    constructor(title, date, description, content, socials) {
      this.title = title;
      this.date = date;
      this.description = description;
      this.content = content;
      this.socials = socials;
    }
  }
  
  
  function createNewMember(title, date, description, content, socials) {
          let newMemer = new Data(title, date, description, content, socials);
          let result = {};
          return result = {
                  &quot;title&quot;: newMemer.title,
                  &quot;date&quot;: newMemer.date,
                  &quot;description&quot;: newMemer.description,
                  &quot;content&quot;: newMemer.content,
                  &quot;socials&quot;: newMemer.socials
          }
  }
  
  export async function toJson(inFile, title, data, description, content, socials) {
    const rl = readline.createInterface({
      input: fs.createReadStream(inFile),
      crlfDelay: Infinity,
    });
    const lines = [];
    for await (const line of rl) {
      lines.push(line);
    }
    let object = createNewMember(title, data, description, content, socials);
    lines.push(JSON.stringify(object));
    let string = lines.join('') + ',';
    writeFile(inFile, string);
  }
  
  function writeFile(inFile, data) {
            fs.writeFile(inFile, data, { flag: &quot;a+&quot; }, (err) =&gt; {
                              if (err) throw err;
                              console.log(&quot;The file has been saved!&quot;);
                            }
                            );
  }
  
  </code></pre>
  <p>my inputs:
  a directory of posts
  <code>~/path/to/posts/post1.md</code>
  <code>~/path/to/posts/post2.md</code>
  <code>~/path/to/posts/post3.md</code></p>
  <p>contents of post (the same for each to make things easy)</p>
  <pre><code>---
  title: Hello this is a test
  date: 05/04/2022
  description: this is a test
  socials:
  ---
  
  # Hello
  ## subhello
  </code></pre>
  <p>my target: <code>~/path/to/post.json</code></p>
  <p>my entry point function:</p>
  <pre><code>const postDir = path.join(process.cwd(), 'posts');
  const destDir = path.join(process.cwd(), '_data');
  const jsonFile = 'post.json';
  
  const fileNames = fs.readdirSync(postDir);
  
  async function generatePosts(fileNames, postDir, destDir, jsonFile) {
            fileNames.forEach( (fileName) =&gt; {
                          const fullPath = path.join(postDir, fileName);
                          const destPath = path.join(destDir, jsonFile)
                          transformMdtoHtmltoJson(destPath, fullPath);
                  });
  }
  
  
  generatePosts(fileNames, postDir, destDir, jsonFile);
  
  </code></pre>
  <p>my output: <code>~/path/to/post.json</code></p>
  <pre><code>{
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null},
  {
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null},
  {&quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null
  },
  </code></pre>
  <p>What I want to do is wrap this output in [ ] brackets so that I can loop through them as an array.</p>
  <p>Desired output:</p>
  <pre><code>[
  {
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null},
  {
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null},
  {&quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null
  },
  ]
  </code></pre>
  <p>I have tried several ways, but they all just wrap each individual post with [ ] resulting in</p>
  <pre><code>[{
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null}],
  [{
  &quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null}],
  [{&quot;title&quot;:&quot;Hello this is a test&quot;,
  &quot;date&quot;:&quot;05/04/2022&quot;,
  &quot;description&quot;:&quot;this is a test&quot;,
  &quot;content&quot;:&quot;&lt;h1&gt;Hello&lt;/h1&gt;\n&lt;h2&gt;subhello&lt;/h2&gt;&quot;,
  &quot;socials&quot;:null
  }],
  </code></pre>
  <p>Which is not what I want. I had thought that maybe doing this with callbacks would be the solution but I have not been able to get it right. Please help.</p>
  <p>Here is a link to the github repo <a href="https://github.com/giannicrivello/MarkdownToHtmlToJson" rel="nofollow noreferrer">https://github.com/giannicrivello/MarkdownToHtmlToJson</a></p>