在 Node.js 中解析 Json(带有数组和对象)并将数据导出到 Excel 文件中

2024-05-18

我是 Node.js 新手。我的要求是,我需要解析 JSON 并将数据导出到 Excel 文件中,其中包含 JSON 中的所有字段。

我的 JSON 如下:

{
  "id": 1255,
  "title": "The Brain and Nervous System (LS1.D)",
  "description": "By the time you finish this playlist, you should be able to: 1. Describe how the nervous system is organized and how it works 2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain",
  "keyTerms": "You should also be able to define the following words: stimulus, response, neuron, sensory neuron, motor neuron, nerve impulse, dendrite, axon, nerve, central nervous system, peripheral nervous system, brain, spinal cord, cerebrum, cerebellum, brain stem, retina, cochlea",
  "visible": true,
  "introduction": {
    "id": 5336,
    "title": "Introductory Materials",
    "resources": [
      {
        "id": 23022,
        "title": "Vocabulary - Brain and Nervous System",
        "description": "",
        "purpose": "",
        "category": "Website",
        "position": 1,
        "contentItem": {
          "id": 1650,
          "url": "http://quizlet.com/45497180/flashcards",
          "itemType": "Website",
          "embedUrl": null
        }
      },
      {
        "id": 23023,
        "title": "The Brain and Nervous System Study Guide",
        "description": "Fill out this study guide while studying! It will help you prepare for the assessment!",
        "purpose": "",
        "category": "Website",
        "position": 2,
        "contentItem": {
          "id": 12581,
          "url": "https://docs.google.com/a/summitps.org/document/d/1TjF1MY3cyGNKT4s46uk1iz5NvjrY59eNPH8YKYYTC_E/edit",
          "itemType": "Website",
          "embedUrl": null
        }
      }
    ]
  },
  "objectives": [
    {
      "id": 10732,
      "title": "1. Describe how the nervous system is organized",
      "caContribution": 5,
      "position": 1,
      "resources": [
        {
          "id": 23024,
          "title": "Reading - How the Nervous System Works",
          "description": "",
          "purpose": "",
          "category": "Document",
          "position": 1,
          "contentItem": {
            "id": 1651,
            "url": null,
            "itemType": "Document",
            "embedUrl": "https://view-api.box.com/1/sessions/493fca96d46a4559813c3118ebeef8b6/view?theme=light",
            "s3Url": "/files/content_items/relateds/000/001/651/original/53d1ddd8f07787731aa7d84f-how_20nervous_20system_20works_001.pdf?1424368501"
          }
        }
      ]
    },
    {
      "id": 10734,
      "title": "2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain",
      "caContribution": 5,
      "position": 2,
      "resources": [
        {
          "id": 23039,
          "title": "Study Jams - The Senses",
          "description": "This series of videos explains the different senses: sight, hearing, taste, touch, and smell",
          "purpose": "",
          "category": "Website",
          "position": 1,
          "contentItem": {
            "id": 1666,
            "url": "http://studyjams.scholastic.com/studyjams/jams/science/human-body/touching.htm",
            "itemType": "Website",
            "embedUrl": null
          }
        },
        {
          "id": 23040,
          "title": "Nervous System: I'm Sensing Something",
          "description": "Format: Article Content: How does the nervous system work and interact with other systems",
          "purpose": "",
          "category": "Website",
          "position": 1,
          "contentItem": {
            "id": 12582,
            "url": "http://www.biology4kids.com/files/systems_nervous.html",
            "itemType": "Website",
            "embedUrl": null
          }
        }
      ]
    }
  ]
}

我用的是json2xls模块,我得到了电子表格,其中包含字段 id、标题、描述、keyTerms、可见、介绍、目标,其中介绍和目标如下[对象][对象],[对象][对象]

下面是导出到Excel文件的代码:

var fs = require("fs");
var json2xls = require("json2xls");

fs.readFile('/home/e100093/nodejs/sampleJson.json','utf8',function(err,body){

var jsonData = JSON.parse(body);

var xls = json2xls(jsonData);
fs.writeFileSync('final-test.xlsx', xls, 'binary');

});

但我想要输出字段,如 id、title、description、keyTerms、visible、introduction.id、introduction.title、introduction.resources.id、introduction.resources.title、...、objectives.id、objectives.title、objectives .描述,......,等等。

我需要动态解析对象和数组并在电子表格的每一列中生成字段。

包含列和数据的示例输出如下:

id  title  description  keyTerms  visible  introduction.id  introduction.title  introduction.resources.id  introduction.resources.title  introduction.resources.description  introduction.resources.purpose  introduction.resources.category  introduction.resources.position  introduction.resources.contentItem.id  introduction.resources.contentItem.url  introduction.resources.contentItem.itemType  introduction.resources.contentItem.embedUrl  objectives.id  objectives.title  objectives.caContribution  objectives.position  objectives.resources.id  objectives.resources.title  objectives.resources.description  objectives.resources.purpose  objectives.resources.category  objectives.resources.position  objectives.resources.contentItem.id  objectives.resources.contentItem.url  objectives.resources.contentItem.itemType  objectives.resources.contentItem.embedUrl  objectives.resources.contentItem.s3Url

1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  5336  Introductory Materials  23022  Vocabulary - Brain and Nervous System  null  null  Website  1  1650  http://quizlet.com/...  Website  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  5336  Introductory Materials  23023  The Brain and Nervous System Study Guide  Fill out this...  null  Website  2  12581  https://docs.google.com/...  Website  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10732  1. Describe how...  5  1  23024  Reading - How...  null  null  Document  1  1651  null  Document  https://view-api.box.com/1...  /files/content...  
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10734  2. Describe the role...  5  2  23039  Study Jams - The Senses  This series of videos  null  Website  1  1666  http://studyjams.scholastic.com...  Website  null
1255  The Brain and Nervous System (LS1.D)  By the time you finish...  You should also...  true  null  null  null  null  null  null  null  null  null  null  null  null  10734  2. Describe the role...  5  2  23040  Nervous System: I'm Sensing... Format: Article Content...  null  Website  1  12582  http://www.biology4kids.com...  Website  null

请帮助我如何做到这一点。


你可以这样做AlaSQL http://github.com/agershun/alasql具有特殊功能的 javascript 库SEARCH运算符,旨在搜索嵌套对象。请参阅下面的代码,它会生成具有您的结构的 Excel 文件(我没有包含所有字段):

var alasql = require('alasql');

var data = [{
   "id": 1255,
   "title": "The Brain and Nervous System (LS1.D)",
   // ...
},
{
   "id": 1256,
    // ...
}];

// Here is search query
alasql('SEARCH / AS @a \
    UNION ALL( \
      introduction AS @b \
      resources / AS @c \
      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \
        @a->keyTerms AS keyTerms, @a->visible AS visible, \
        @b->id as [introduction.id], @b->title as [introduction.title], \
        @c->id AS [introduction.resources.id], \
        @c->contentItem->id AS [introduction.resources.contentItem.id] \
      ) \
    , \
      objectives AS @b \
      resources / AS @c \
      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \
        @a->keyTerms AS keyTerms, @a->visible AS visible, \
        @b->id as [objectives.id], @b->title as [objectives.title], \
        @c->id AS [objectives.resources.id], \
        @c->contentItem->id AS [objectives.resources.contentItem.id] \
      ) \
    ) INTO XLSX("test411.xlsx",{headers:true})\
    FROM ?',[data]);

一些解释:

  • SEARCH - 是查询嵌套对象的特殊语句
  • / - 循环数组元素
  • UNION ALL(...,...) - 所有嵌套找到记录的并集
  • AS @variable - 将当前搜索位置保存到临时变量
  • 介绍——深入了解房产“介绍”
  • RETURN(...,...) - 创建具有属性的记录(JSON 对象)
  • RETURN(value AS alias) - 值的别名
  • @a->id - 获取 id 属性,类似于 JavaScript 中的 a.id
  • [...] - 带有任何特殊字符的术语
  • INTO XLSX("test411.xlsx",{headers:true}) - 将结果保存到带有标题的 Excel 文件
  • 从 ? - 从参数中获取数据
  • alasql(sql, [data]) - 将数据变量作为查询的第一个参数

您还需要添加其他列来完成此查询。示例文件将被上传here https://github.com/agershun/alasql/blob/develop/test/test411.js在两小时。

您还可以从源代码中删除这一行:INTO XLSX(), and alasql()将返回一个包含所有属性的 JSON 对象:

var res = alasql('SEARCH / AS @a UNION ALL(...) FROM ?',[data]);
console.log(res);

这里是jsFiddle 示例 http://jsfiddle.net/agershun/ws0vewj0/2/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Node.js 中解析 Json(带有数组和对象)并将数据导出到 Excel 文件中 的相关文章

  • ASP.NET 验证控件和 Javascript 确认框

    我有一个使用 NET 服务器端输入验证控件的页面 此页面还有一个 javascript 确认框 在提交表单时会触发该确认框 当前 当选择 提交 按钮时 会出现 javascript 确认框 一旦确认 就会触发 ASP NET 服务器端验证控
  • 是否有任何理由使用 axios 而不是 ES6 fetch [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 研究了 axios 和 ES6 fetch 的文档 我发现两者非常相似 并且都受到 ajax 及其简写的强烈影响 axios 的主要优点是浏览器
  • 如何使用 Nextjs/React 将 JSON 对象导出到 Excel?

    我有一个检索 json 对象的端点 如下所示 data id 1 temaIndicador Indian codigo 001 observaciones Interactions Specialist tertiary Regional
  • 为某个时刻添加持续时间 (moment.js)

    时刻版本 2 0 0 阅读文档后 http momentjs com docs manipulating add 我认为这很简单 Chrome 控制台 var timestring1 2013 05 09T00 00 00Z var tim
  • 使用nodejs的sequelize更新多对多连接表

    我有一个产品表和一个类别表 一个产品可以有多个类别 一个类别可以有多个产品 因此我有一个 ProductsCategories 表来处理多对多连接 在下面的示例中 我尝试将我的一款产品 ID 为 1 与 3 个不同的类别 ID 为 1 2
  • 为什么这个递归函数返回未定义?

    我正在尝试编写一个使用递归组合两个字符串的函数 我的代码如下 但我不知道为什么该函数返回未定义 特别是当我在基本情况下使用 console log 时 它不会打印未定义而是打印正确的值 var str3 function merge str
  • Mongoose 总是返回空数组? [复制]

    这个问题在这里已经有答案了 我是nodejs i的新手 我已经有一个名为aqi的数据库 集合名称为pln 我试图在网页上显示集合中的所有记录 但猫鼬总是返回空数组 我已经用其他数据库测试了它 但我可以从它们获取数据 但对于 pln mong
  • 基于范围内变量的角度设置形式动作

    我一直在尝试设置一个搜索表单 可以在其中注入表单操作属性 在我的表格中我有
  • 从浏览器访问本地文件?

    您好 我想从浏览器访问系统的本地文件 由于涉及大量安全检查 是否可以通过某种方式实现这一目标 或使用 ActiveX 或 Java Applet 的任何其他工作环境 请帮帮我 要通过浏览器访问本地文件 您可以使用签名的 Java Apple
  • 为什么 TypeScript 混合了模块和原型模式?

    我正在查看此页面上 TypeScript 生成的 JS 代码 http www typescriptlang org Playground http www typescriptlang org Playground 基本上 要创建一个Gr
  • JavaScript setTimeout 和更改系统时间会导致问题

    我注意到如果我设置setTimeout未来1分钟 然后将我的系统时间更改为过去5分钟 setTimeout功能将在 6 分钟后触发 我这样做是因为我想看看夏令时系统时钟更改期间会发生什么 我的 JavaScript 网页使用setTimeo
  • 如何捕获文本区域上的 Enter 按键而不是 Shift+Enter? [复制]

    这个问题在这里已经有答案了 I m doing it for texarea A function should be called when the user press Enter but nothing should be done
  • 如何计算一行中Flexbox项目的数量?

    网格是使用 CSS flexbox 实现的 Example http jsbin com jumosicasi edit html css js output 本示例中的行数为 4 因为我出于演示目的固定了容器宽度 但是 实际上 它可以根据
  • jQuery UI 对话框 - 关闭后无法打开

    我有一个问题jquery ui dialog box https jqueryui com dialog 问题是 当我关闭对话框然后单击触发它的链接时 除非刷新页面 否则它不会再次弹出 如何在不刷新实际页面的情况下回调对话框 下面是我的代码
  • 节点未找到全局模块

    所以我意识到这是一个相当通用的标题和问题 但我已经搜索了很多答案 但遗憾的是它们似乎都不适合我 我希望通过我自己提供更多信息 也许有人有一个具体的答案 或者确切地知道将我重定向到哪个答案 我的问题 当我全局安装节点模块时 例如npm ins
  • eventSources 到事件 Json,完整日历

    我正在尝试从 eventSources 获取 json 调用到我的事件 我在 eventSources 中返回的 json 是 title Title Test start 1305841052 当我将此字符串传递到事件中时 它会正确显示日
  • Socket.io、集群、快速和同步事件

    1周以来我遇到了一个大问题 我尝试将实际在单核上运行的 node JS 项目转换为具有集群的多核 对于 websockets 目前 我对事件没有任何问题 但是 对于 xhr polling 或 jsonp polling 我在集群模式下使用
  • 从json中获取所有子节点

    我有以下 json var source k 01 k 02 children k 05 k 06 children k ABC k PQR k 07 k 03 我希望能够指定 k 的值并取回所有孩子 以及孙
  • 如何强制下载图片?

    我的页面上有一个动态生成的图像 如下所示 img src 我不想告诉我的用户右键单击图像并点击保存 而是想公开一个下载链接 单击该链接将提示下载图像 如何实现这一目标 最初我在 js 中尝试这样做 var path my image att
  • 拉斐尔路径交叉点不起作用

    我对拉斐尔和 pathIntersection method JSFiddle 示例 http jsfiddle net t6gWt 2 您可以看到有两条线都与曲线相交 但当我使用 pathIntersection method 有一个未解

随机推荐