AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1063795
Accepted
safect
safect
Asked: 2021-05-18 00:39:27 +0800 CST2021-05-18 00:39:27 +0800 CST 2021-05-18 00:39:27 +0800 CST

使用嵌套值将 JSON 转换为 CSV 的问题

  • 772

到目前为止,我过去将标准 JSON 转换为 CSV 没有问题jq。但是现在我的数据变得复杂了。

主要的基本标签是account和subaccount。所有其他值都是变量。该条目year具有可变值并经常随时间更改。这是我无法解决的任务。

这是输出

{
    "jsonrpc": "2.0",
    "result": {
        "current": [{
            "number": 171883808,
            "commission": 10,
            "year": [
                [1999, 9224, 0],
                [2000, 41919, 9224],
                [2001, 162945, 41919],
                [2002, 397993, 162945],
                [2003, 751570, 397993],
                [2004, 886466, 751570]
            ],
            "status": true,
            "last": 9782473,
            "account": "VFUIJOPQW",
            "subaccount": "BLPORDGS"
        }, {
            "number": 69999012,
            "commission": 15,
            "year": [
                [2012, 97587, 0],
                [2013, 472685, 97587],
                [2014, 605963, 472685],
                [2015, 698634, 605963],
                [2016, 1931094, 1745922]
            ],
            "status": true,
            "last": 9782490,
            "account": "VFUIJOXXX",
            "subaccount": "BLPORXXX"
        }],
    "id": 1
    }
}
csv json convert jq
  • 1 1 个回答
  • 236 Views

1 个回答

  • Voted
  1. Best Answer
    Thor
    2021-05-18T03:13:09+08:002021-05-18T03:13:09+08:00

    我假设您的意思是输出一个带有account,subaccount和year数组的表。您可以使用更新赋值运算符 ( |=) 执行此操作,例如:

    jq -r '.result | .current | .[]                 |
           .account    as $account                  |
           .subaccount as $subaccount               |
           .year[] |= ([$account, $subaccount] + .) | 
           .year[] | @tsv'
    

    输出:

    VFUIJOPQW  BLPORDGS  1999  9224     0
    VFUIJOPQW  BLPORDGS  2000  41919    9224
    VFUIJOPQW  BLPORDGS  2001  162945   41919
    VFUIJOPQW  BLPORDGS  2002  397993   162945
    VFUIJOPQW  BLPORDGS  2003  751570   397993
    VFUIJOPQW  BLPORDGS  2004  886466   751570
    VFUIJOXXX  BLPORXXX  2012  97587    0
    VFUIJOXXX  BLPORXXX  2013  472685   97587
    VFUIJOXXX  BLPORXXX  2014  605963   472685
    VFUIJOXXX  BLPORXXX  2015  698634   605963
    VFUIJOXXX  BLPORXXX  2016  1931094  1745922
    
    • 0

相关问题

  • 使用 import-Csv 导入 pc 列表并使用 powershell 脚本更改属性

  • sed 中的正则表达式:匹配前面或后面没有特定其他字符的字符

  • Powershell - 在域中的所有 PC 上生成所有本地连接的打印机的 CSV

  • 在命令行上从 csv 文件中过滤带有空格字符的字段

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve