1个大型json文件到许多单独的降价文件

时间:2019-03-28 19:19:37

标签: json graphql markdown

这是一篇有关上下文的文章,但是要点是我有一个包含多个字段的2000项目json文件,我需要将它们全部变成自己的markdown文件,转换除定义项目外的每个内部字段成为首要问题。如果我也可以删除内容项,那将是一个好处。

我该如何处理?我已经三天把头撞在墙上了。

{
"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"
  },
}

当前方向。这很可怕,但是这使我成为了我需要的第一要务。接下来,我需要弄清楚如何仅附加数据的描述部分。

const fs = require("fs");
const YAML = require("yamljs");
// const json2md = require("./json2md.js");
const bigObject = require("./data1.json");

// var showdown = require("showdown"),
//   converter = new showdown.Converter(),
//   text = "# hello, markdown!",
//   html = converter.makeHtml(text);

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


使用网站转换为降价后,我手工完成的一个文件的输出。它通常由隐藏在最前面的所有内容组成,而定义选项卡显示在主体内部

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.




1 个答案:

答案 0 :(得分:0)

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!`);
          // });
        });
      }
    );
  });
});