【问题标题】:How to automatically process sqlite export to get JSON that reflect relations DB?如何自动处理 sqlite 导出以获取反映关系数据库的 JSON?
【发布时间】:2015-04-17 14:36:39
【问题描述】:

我正在从sqlite database(这里没有选择)导出数据以为 AngulaJS 应用程序生成 JSON。

从 SQLite 导出 JSON

sqlite3CSV 导出展平关系结构并添加重复

[
  {
    "bid": 5, // book id
    "aid": 4,  // author's id
    "tags": 3,
    "title": "Jacques le fataliste et son maître",
  },
  {
    "bid": 5,
    "aid": 23, // same book another author
    "tags": 8, // same book another tag
    "title": "Jacques le fataliste et son maître",
  }
  …
]

SQLite 转 JSON 命令

sqlite database 可以在 github 上找到,我用来导出/转换的命令是:

sqlite3 -csv -header app/data/data.sqlite3 \
"SELECT b.id as bid, title, b.sort as sort_book, a.id as aid, a.sort as sort_author, path, name FROM books as b inner join books_authors_link as b_a ON b.id = b_a.book INNER JOIN authors as a ON a.id = b_a.author" \
| ./node_modules/csvtojson/bin/csvtojson \
> authors-books.json

JSON 目标

我与客户一起以面向文档的方式设计了最终的 JSON:

  • 我在一个对象中所需要的一切;
  • 对其他资源的引用为
    • 外键的id/主键
    • 多对多关系的_id_s/主键列表。

这是一个例子:我的目标

[
    {
        "id": 2,
        "authors": [4, 23],
        "tags": [3,8,29,69],
        "title": "Jacques le fataliste et son maître",
        …
    }
    …
]

如果你说法语,这里是github issue from the project

问题

那么如何使用命令行管道或基于 javascript 的工具获得此结果?

【问题讨论】:

    标签: json sqlite underscore.js export-to-csv jq


    【解决方案1】:

    使用jq,您可以做到这一点。

    按关键字bid(也可能是title)对项目进行分组,并聚合作者和标签。

    group_by(.bid) |
        map({
            bid:     .[0].bid,
            title:   .[0].title,
            authors: map(.aid),
            tags:    map(.tags)
        })
    

    【讨论】:

      【解决方案2】:

      在节点中使用您的数据和此代码似乎可以解决问题。它使用 lodash(更好的 underscore.js),我希望它符合你所说的基于 javascript 的工具:)

      var _ = require('lodash');
      
      var arrayFields = [
        'aid'
      ];
      
      var keysMap = {
        id: 'bid',
        authors: 'aid',
        tags: 'tags',
        title: 'title'
      };
      
      var result = _(rows)
        .groupBy('bid') // group the rows by id
        .map(function(rows) {
          // for each group, take the first instance of the row as a basis
          var row = _.clone(_.first(rows));
          // and collect all grouped row values for the interesting fields
          _.forEach(arrayFields, function(key) {
            row[key] = _.pluck(rows, key);
          })
          return row;
        })
        .map(function(row) {
          var obj = {};
          // build a row object with translated keys
          _.forEach(keysMap, function(originalKey, targetKey) {
            obj[targetKey] = row[originalKey];
          });
          return obj;
        })
        .value();
      
      console.log(result);
      

      这是输出:

      [ 
        { 
          id: 5,
          authors: [ 4, 23 ],
          tags: 3,
          title: 'Jacques le fataliste et son maître' 
        } 
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        • 2021-08-12
        • 2019-12-03
        • 2018-05-24
        • 2013-01-23
        • 1970-01-01
        • 2017-09-05
        相关资源
        最近更新 更多