json数据转换成表格

Like many of you, I often have to take the result of SQL queries and convert the rowsets to JSON data objects. Sometimes I have to do the same with CSV files from spreadsheets. The transformation process can be a hassle, though anyone can do it. Yet, it can be time-consuming and error-prone. This post will show you how to use the treeize Node.js package to simplify the process in very few lines of code.

像你们中的许多人一样,我经常不得不获取SQL查询的结果并将行集转换为JSON数据对象 。 有时,我必须对电子表格中的CSV文件执行相同的操作。 转换过程可能很麻烦,尽管任何人都可以做到。 但是,这可能既耗时又容易出错。 这篇文章将向您展示如何使用treeize Node.js包仅需几行代码即可简化该过程。

Before going further, I’ll first need a dataset to base some examples on. The domain will be Books, which lend themselves to all sorts of categorization. I will use a fake data generator called casual, which I previously used for mocks in my post on GraphQL testing.

在继续之前,我首先需要一个数据集以一些示例为基础。 领域将是Books ,这使它们可以进行各种分类。 我将使用一个称为Casual的伪造数据生成器,我先前在GraphQL测试中的帖子中使用过该模型 。

The book data will be of the following structure:

图书数据将具有以下结构:

casual.define('book', () => {const author = casual.random_element(authors);const book = {first_name: author.first,last_name: author.last,title: casual.random_element(author.titles),category: casual.random_element(author.category)}return book;
});

Every time I request a casual.book I get a book with a new set of values. It’s not entirely random. The generator uses some predefined data for well-known authors, and more-or-less randomly generated data for other authors. Here’s a sample:

每次我请求casual.book我都会得到一本casual.book新值的书。 这不是完全随机的。 生成器为知名作者使用一些预定义的数据 ,为其他作者使用或多或少随机生成的数据。 这是一个示例:

{ dataset:[ { first_name: 'Barbara',last_name: 'Cartland',title: 'The Pirate and the Piano Teacher',category: 'thriller' },{ first_name: 'Carlie',last_name: 'Haley',title: 'Digitized Global Orchestration',category: 'engineering' },{ first_name: 'Arthur',last_name: 'Doyle',title: 'The Case of the Spotted Dick',category: 'mystery' },{ first_name: 'Reinhold',last_name: 'Gutmann',title: 'Managed Directional Benchmark',category: 'management' },{ first_name: 'Isaac',last_name: 'Asimov',title: 'Once in a Venusian Sun',category: 'science fiction' },{ first_name: 'R. L.',last_name: 'Stein',title: 'Why are You Scared of Me?',category: 'childrens books' },{ first_name: 'Alicia',last_name: 'Cruickshank',title: 'Balanced Local Database',category: 'engineering' },{ first_name: 'Chase',last_name: 'Runte',title: 'Ergonomic Tertiary Solution',category: 'engineering' } ] }

If you’re interested in how this data was generated, the full source code used in this post can be found here. For a little bit of added realism, this generated data will be thrown into an in-memory SQL database for later retrieval. Here’s the format of the results for the SQL query:

如果您对如何生成这些数据感兴趣,可以在此处找到本文中使用的完整源代码。 为了增加一点真实感,此生成的数据将被扔到内存中SQL数据库中,以便以后进行检索。 这是SQL查询结果的格式:

SELECT title, category, first_name, last_name
FROM book
JOIN author ON author.id = book.author

This format is, for all intents and purposes, identical to the format of the dataset shown just previously, for example:

出于所有目的和目的,此格式与之前显示的数据集的格式相同,例如:

[ { title: 'Proactive Regional Forecast',category: 'mystery',first_name: 'Arthur',last_name: 'Doyle' },{ title: 'More Scary Stuff',category: 'suspense',first_name: 'Steven',last_name: 'King' },{ title: 'Scary Stuff',category: 'occult',first_name: 'Steven',last_name: 'King' },{ title: 'Persistent Neutral Info Mediaries',category: 'management',first_name: 'Maegan',last_name: 'Frami' },{ title: 'Enhanced Background Frame',category: 'engineering',first_name: 'Winifred',last_name: 'Turner' },...

The main difference between the dataset and the rowset is that when populating the database from the casual-generated data, I eliminated duplicate authors (by name) and book titles (by category):

数据集和行集之间的主要区别在于,从临时生成的数据填充数据库时,我消除了重复的作者(按名称)和书名(按类别):

转换为JSON (Converting to JSON)

You might notice that the dataset results were in JSON format already. What this post aims for, though, is to build a containment hierarchy that shows the relationships between authors, books, and categories in a concise way. That’s not the case with the rowset values, where the results are glorified key-value pairs, where each pair is a column name and value from a table row.

您可能会注意到数据集结果已经是JSON格式。 但是,这篇文章的目的是建立一个包含层次结构,以简洁的方式显示作者,书籍和类别之间的关系。 行集值不是这种情况,其中的结果是美化的键值对,其中每个对都是列名和表行中的值。

So, for example, say I want to list authors, the categories they write in, and the titles of books in those categories that they authored. I want to show each category just once, and each book within each category should be listed only once, also.

因此,例如,说我想列出作者,他们所写的类别以及他们所创作的那些类别中的书名。 我只想显示每个类别一次,并且每个类别中的每一本书也应该只列出一次。

This is a pretty common type of reducing operation that is often applied to rowset data. One way to conquer the problem is to declare a container object, then populate it by looping through the rowsets. A typical implementation might be:

这是一种非常常见的归约操作类型,通常应用于行集数据。 解决问题的一种方法是声明一个容器对象,然后通过遍历行集来填充它。 典型的实现可能是:

The handrolled()method gets a bit hairy the deeper the hierarchy. Local variables are used to reduce long path lengths. We have to keep the meta-structure in mind to write the proper initializations of properties in the JSON object. What could be simpler?

handrolled()方法在层次结构越深时变​​得有些毛茸茸。 局部变量用于减少长路径长度。 我们必须牢记元结构,以便在JSON对象中编写属性的正确初始化。 有什么可能更简单?

The results returned are:

返回的结果是:

..."Doyle,Arthur": {"categories": {"thriller": {"titles": ["The Case of the Spotted Dick","The Case of the Mashed Potato"]},"mystery": {"titles": ["The Case of the Spotted Dick"]}}},"Asimov,Isaac": {"categories": {"science": {"titles": ["Once in a Venusian Sun","Total Multi Tasking Forecast"]},"general interest": {"titles": ["Total Multi Tasking Forecast","Once in a Venusian Sun","Fourth Foundation"]}}},"Kilback,Bradley": {"categories": {"management": {"titles": ["Mandatory Solution Oriented Leverage"]},"engineering": {"titles": ["Multi Layered Fresh Thinking Framework","Total Scalable Neural Net","Mandatory Solution Oriented Leverage"]},"reference": {"titles": ["Multi Layered Fresh Thinking Framework"]}}},...

用Treeize构建一棵树 (Building a tree with Treeize)

The npm module treeize is designed to simplify the conversion of rowsets to structured JSON data through the use of descriptive keys. Installation through npm is per usual:

npm模块树化 旨在通过使用描述性键简化行集到结构化JSON数据的转换。 通常通过npm安装:

npm install --save treeize

JSON行集 (JSON Rowsets)

Treeize is able to recognize reoccurring patterns in the rowsets. It transforms them according to how the key names are defined in metadata passed in as the seed structure. Here’s the code:

Treeize能够识别行集中的重复模式。 它根据如何在作为种子结构传入的元数据中定义关键字名称来对其进行转换。 这是代码:

This is about a dozen lines of code compared to double that for the hand-rolled version. Notice the key values used in the mapping operation. Treeize recognizes plurals as collections, so categoriesand titleswill be arrays. The colons (‘:’) in the names indicate nesting. Typewill be a property of an object in the array of categories, and namewill be a property in all objects in titles.

与手动版本相比,这大约是十几行代码。 请注意映射操作中使用的键值。 Treeize将复数形式识别为集合,因此categoriestitles将是数组。 名称中的冒号(':')表示嵌套。 Type将是类别数组中对象的属性, name将是标题中所有对象的属性。

The tree is built when authors.grow(seed) is called, and the results retrieved through authors.getData(). However, it doesn’t quite yield the same results as what we had from the hand-rolled method:

该树是在调用authors.grow(seed)并通过authors.getData()检索结果时构建的。 然而,它并不完全产生相同的结果,我们从手卷方法有:

...,
{"name": "Glover, Ashley","categories": [{"type": "engineering","titles": [{"name": "Intuitive Full Range Capacity"},{"name": "Organic Encompassing Core"}]},{"type": "reference","titles": [{"name": "Distributed Client Server Service Desk"},{"name": "Organic Encompassing Core"}]},{"type": "management","titles": [{"name": "Organic Encompassing Core"}]}]
},...

One notable difference is that categories are not named objects (as before), but objects with a name property. Title is also not just an array of strings, but an array of objects with nameas the title. Treeize interprets categories and titles as arrays of objects, not as maps (or arrays of primitives). For most use cases, this is not much of an issue. But, if you need to find a category by name quickly (rather than iterate through an array of categories), then you can take care of that through a couple of reduce operations to arrive at the same structure as before:

一个显着的区别是类别不是像以前一样命名的对象,而是具有name属性的对象。 Title不仅是一个字符串数组,而且是一个以name为标题的对象数组。 Treeize将categoriestitles解释为对象数组,而不是地图(或基元数组)。 对于大多数用例来说,这不是什么大问题。 但是,如果您需要按名称快速查找类别(而不是遍历一系列类别),则可以通过几次归约操作来达到与以前相同的结构:

,...   "Doyle, Arthur": {"categories": {"mystery": {"titles": ["The Case of the Spotted Dick","Pre Emptive Needs Based Approach","The Case of the Mashed Potato"]},"thriller": {"titles": ["The Case of the Mashed Potato","The Pound Puppies of the Baskervilles"]}}},...

试算表 (Spreadsheets)

Sometimes data comes from spreadsheets rather than relational databases. Treeize is adept at handling this case, too. Instead of using descriptive keys as we did with rowset data in JSON format, the same descriptive format is used as column values in a header row:

有时数据来自电子表格,而不是关系数据库。 Treeize也擅长处理这种情况。 与使用JSON格式的行集数据一样,不使用描述性键,而是将相同的描述性格式用作标题行中的列值:

var seed = [
['name', 'categories:type', 'categories:titles:name'],
['Doyle, Arthur', 'mystery', 'The Adventure of the Gyring Gerbils'],
['Schuppe, Katarina', 'engineering', 'Configurable Discrete Locks'],
['Doyle, Arthur', 'mystery', 'Holmes Alone 2'],
['Asimov, Isaac', 'science fiction', 'A Crack in the Foundation']
];// same as before...
var authors = new Treeize();
authors.grow(seed);
return authors.getData();

There are quite a few options that treeize supports, and I’ve only shown the basics. It is a powerful tool that makes light work of transforming row-based data structures.

treeize支持的选项有很多,而我仅展示了基础知识。 它是一个强大的工具,可以轻松地转换基于行的数据结构。

Complete source can be found at my GitHub.

完整的源代码可以在我的GitHub上找到 。

翻译自: https://www.freecodecamp.org/news/spreadsheets-and-rowsets-getting-you-down-fd6ff7599052/

json数据转换成表格

json数据转换成表格_电子表格会让您失望吗? 将行数据转换为JSON树很容易。相关推荐

  1. json数据转换成excel表格

    在工作中遇到 一个需要把json数据转换成excel的功能,于是网上去搜索在线的工具,还蛮多的, 找了一大圈都不是特别满意,很多都是转了之后就是一个排列好的数据,需要自己复制到excel,类似下图这样 ...

  2. 使用 pqgrid 将JSON数据转换成TABLE

    使用 pqgrid 将JSON数据转换成TABLE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http: ...

  3. php将json转化成数组,PHP怎么把JSON数据转换成数组?

    在PHP中可以使用"json_decode"函数把JSON数据转换成数组,该函数的语法是"json_decode( $json, $assoc=FALSE, $depth ...

  4. 后台返回的数组包对象格式的数据转换成表格数据格式的方法

    后台返回的数组包对象格式的数据转换成表格数据格式的方法 transformDate(res) {const mapInfo = {id: '编号',password: '密码',mobile: '手机 ...

  5. 表格数据转换为json格式 python

    在日常的工作学习中,特别是对于码农以及一些经常与数据打交道的朋友来说,经常需要将一些表格数据转换为json格式数据.其实对于一些少量的表格数据,可以直接利用excel的"Excel to J ...

  6. 将txt文本数据转换为json对象

    nodejs 将txt文本数据转换为json对象 1.准备 文件夹准备如下: 2.实现 a.txt中的内容: 小王,19 小李,20 小陈,21 work.js中的内容: // 导入fs.path c ...

  7. [工具库]JOJSONBuilder工具类——一键把多个bean对象数据转换为JSON格式数据

    本人大四即将毕业的准程序员(JavaSE.JavaEE.android等)一枚,小项目也做过一点,于是乎一时兴起就写了一些工具. 我会在本博客中陆续发布一些平时可能会用到的工具. 代码质量可能不是很好 ...

  8. JavaScript数组格式的数据转换为json格式数据

    vue + JavaScript 把数组格式的数据转换为json格式数据 <!DOCTYPE html> <html lang="en"><head& ...

  9. 将JSON数据转换成JAVA的实体类

    思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型) Java实体类: SearchFilter 类 1 publ ...

最新文章

  1. 最大连续子序列(dp)
  2. JavaScript 30 - 3 学习笔记
  3. 使用netsh命令来管理IP安全策略(详细介绍)
  4. 微信小程序页面搜索框查询(无后台接口情况下)
  5. 简谈C/C++学习路线
  6. springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls
  7. 论文浅尝 | Interaction Embeddings for Prediction and Explanation
  8. 【短文本相似度】传统方法BM25解决短文本相似度问题
  9. zabbix server配置文件
  10. js高级编号笔记[新]-访问文档对象
  11. iOS UISlider
  12. python实现水仙花数
  13. Linux 添加中文字体库
  14. 浏览器全面禁止第三方Cookie
  15. C++11特性及其它常用特性
  16. coursera python证书_Coursera证书|三天零基础Python编程入门
  17. 时间序列预测11:用电量预测 01 数据分析与建模
  18. 如何在Windows 7、8或10中恢复快速启动栏
  19. wmi服务或wmi提供程序_什么是WMI提供程序主机(WmiPrvSE.exe),为什么使用那么多的CPU?...
  20. 我的择业思考:在AI最火的时候来到工业界!

热门文章

  1. 面试大厂应该注意哪些问题?算法太TM重要了
  2. 小企业服务器设置位置,小企业服务器配置
  3. 2019 Multi-University Training Contest 1 - 1001 - Blank - dp
  4. eclipse类自动生成注释
  5. wget命令下载文件
  6. javascript 减少回流
  7. python 下字符串格式时间比较
  8. git学习相关的博客地址
  9. error C2440 “static_cast” 无法从“void (__thiscall CPppView )(void)”转换为“LRESULT (__thiscall
  10. Android代码抄袭Java曝猛料 新证据出现