1个大json文件到多个单独的标记文件

1个大json文件到多个单独的标记文件,json,graphql,markdown,Json,Graphql,Markdown,这是一篇关于上下文的长篇文章,但其要点是我有一个包含多个字段的2000项json文件,我需要使它们都成为自己的标记文件,将每个内部字段(定义项除外)转换为frontmatter。如果我也可以删除内容项,那将是一个奖励 我该怎么办?我已经把头撞在墙上三天了 { "3350" : { "askedHistPercent1" : "98", "askedHistYear1" : "2009", "author" : " Name ", "categories" : [

这是一篇关于上下文的长篇文章,但其要点是我有一个包含多个字段的2000项json文件,我需要使它们都成为自己的标记文件,将每个内部字段(定义项除外)转换为frontmatter。如果我也可以删除内容项,那将是一个奖励

我该怎么办?我已经把头撞在墙上三天了

{
"3350" : {
    "askedHistPercent1" : "98",
    "askedHistYear1" : "2009",
    "author" : " Name ",
    "categories" : [ "Generic Category" ],
    "content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
    "date" : "2013-05-30 02:23:04",
    "definition" : "<p>Very long definition section;</p>",
    "excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
    "id" : 3356,
    "permalink" : "",
    "sourceLink1" : "Source",
    "title" : "Title item name"
  }, 
 "3351" : {
    "askedHistPercent1" : "98",
    "askedHistYear1" : "2009",
    "author" : " Name ",
    "categories" : [ "Generic Category" ],
    "content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
    "date" : "2013-05-30 02:23:04",
    "definition" : "<p>Very long definition section;</p>",
    "excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
    "id" : 3356,
    "permalink" : "",
    "sourceLink1" : "Source",
    "title" : "Title item name"
  },
  "3356" : {
    "askedHistPercent1" : "98",
    "askedHistYear1" : "2009",
    "author" : " Name ",
    "categories" : [ "Generic Category" ],
    "content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
    "date" : "2013-05-30 02:23:04",
    "definition" : "<p>Very long definition section;</p>",
    "excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
    "id" : 3356,
    "permalink" : "",
    "sourceLink1" : "Source",
    "title" : "Title item name"
  },
}
一个文件的输出,我在使用网站转换为降价后手动完成。它通常由隐藏在frontmatter中的所有内容组成,而definition选项卡显示在主体内部

3350.md


---
title: ATitleNameHere
categories: Category1
property: 3350
id: 3350
date: 2013-05-30
author: Author Name
description: Category1
askedHistPercent1: 75
askedHistPercent2: 76
askedHistYear1: 2012
askedHistYear2: 2009
assocMediaLink1: Optional section here
assocMediaCaption1: Second Optional section here
---


Definition item here. Usually quite long.





欢迎来到堆栈溢出。具体来说,你有什么问题?您知道如何读取JSON并遍历数据吗?您知道如何输出每个部分的降价吗?请阅读。(同样,这不是有效的JSON。看起来它可能应该被包装在一个顶级的
{}
中,使其成为一个大对象。)啊,你好,克里斯。你是对的,当我复制一些数据时,我忘了在那里添加上括号。我在遍历数据时遇到了麻烦。我已经找到了fs,我相信它能找到答案,现在我正试图把这个大json对象整合起来,将它转换成markdown,然后将它保存到json段的名称中。这是非常非功能性的,但我认为这是正确的方向?请您提供一个示例,说明您希望您的输出是什么样子的,仅针对您的一个输入JSON对象?当然。添加到上述项的末尾将导致堆栈溢出。具体来说,你有什么问题?您知道如何读取JSON并遍历数据吗?您知道如何输出每个部分的降价吗?请阅读。(同样,这不是有效的JSON。看起来它可能应该被包装在一个顶级的
{}
中,使其成为一个大对象。)啊,你好,克里斯。你是对的,当我复制一些数据时,我忘了在那里添加上括号。我在遍历数据时遇到了麻烦。我已经找到了fs,我相信它能找到答案,现在我正试图把这个大json对象整合起来,将它转换成markdown,然后将它保存到json段的名称中。这是非常非功能性的,但我认为这是正确的方向?请您提供一个示例,说明您希望您的输出是什么样子的,仅针对您的一个输入JSON对象?当然。添加到上述项目的末尾

---
title: ATitleNameHere
categories: Category1
property: 3350
id: 3350
date: 2013-05-30
author: Author Name
description: Category1
askedHistPercent1: 75
askedHistPercent2: 76
askedHistYear1: 2012
askedHistYear2: 2009
assocMediaLink1: Optional section here
assocMediaCaption1: Second Optional section here
---


Definition item here. Usually quite long.




const fs = require("fs");
const YAML = require("yamljs");
const bigObject = require("./1111data.json.js");
var _ = require("lodash");

Object.entries(bigObject).forEach(([name, data]) => {
  fs.writeFile(`${_.kebabCase(data.title)}.md`, "---\r\n", err => {
    fs.appendFile(
      `${_.kebabCase(data.title)}.md`,
      YAML.stringify(data),
      err => {
        fs.appendFile(`${_.kebabCase(data.title)}.md`, "\r\n---\r\n", err => {
          // fs.appendFile(`${_.kebabCase(data.title)}.md`, YAML.stringify(data.definition), err => {
          if (err) throw err;
          console.log(`${_.kebabCase(data.title)}.md has been saved!`);
          // });
        });
      }
    );
  });
});