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
    • 最新
    • 标签
主页 / unix / 问题 / 570548
Accepted
Philip Kirkbride
Philip Kirkbride
Asked: 2020-03-02 07:14:12 +0800 CST2020-03-02 07:14:12 +0800 CST 2020-03-02 07:14:12 +0800 CST

将 JSON 数组转换为 CSV

  • 772

我正在寻找将 JSON 转换为 CSV 的解决方案。似乎大多数解决方案都希望 JSON 是单个对象而不是对象数组。

我从这里尝试过的所有解决方案似乎都与我的输入不同,这些输入来自curling this site。

jq当输入是数组而不是对象时,如何使用或其他工具将 JSON 转换为 CSV 。

[
  {
    "id": "4",
    "link": "https://pressbooks.online.ucf.edu/amnatgov/",
    "metadata": {
      "@context": "http://schema.org",
      "@type": "Book",
      "name": "American Government",
      "inLanguage": "en",
      "copyrightYear": "2016",
      "disambiguatingDescription": "The content of this textbook has been developed and arranged to provide a logical progression from the fundamental principles of institutional design at the founding, to avenues of political participation, to thorough coverage of the political structures that constitute American government. The book builds upon what students have already learned and emphasizes connections between topics as well as between theory and applications. The goal of each section is to enable students not just to recognize concepts, but to work with them in ways that will be useful in later courses, future careers, and as engaged citizens. ",
      "image": "https://pressbooks.online.ucf.edu/app/uploads/sites/4/2020/01/American-Government.png",
      "isBasedOn": "https://ucf-dev.pb.unizin.org/pos2041",
      "author": [
        {
          "@type": "Person",
          "name": "OpenStax"
        }
      ],
      "datePublished": "2016-01-06",
      "copyrightHolder": {
        "@type": "Organization",
        "name": "cnxamgov"
      },
      "license": {
        "@type": "CreativeWork",
        "url": "https://creativecommons.org/licenses/by/4.0/",
        "name": "CC BY (Attribution)"
      }
    },
    "_links": {
      "api": [
        {
          "href": "https://pressbooks.online.ucf.edu/amnatgov/wp-json/"
        }
      ],
      "metadata": [
        {
          "href": "https://pressbooks.online.ucf.edu/amnatgov/wp-json/pressbooks/v2/metadata"
        }
      ],
      "self": [
        {
          "href": "https://pressbooks.online.ucf.edu/wp-json/pressbooks/v2/books/4"
        }
      ]
    }
  }
]

所需格式:

id, link, context, type, name, inLanguage, image, author_type, author_name, license_type, license_url, license_name
json csv
  • 2 2 个回答
  • 6446 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2020-03-02T10:02:02+08:002020-03-02T10:02:02+08:00

    问题并不是您显示的 JSON 是一个数组,而是数组的每个元素(您只有一个)都是一个相当复杂的结构。直接将每个数组条目中的相关数据提取到一个较短的平面数组中,然后使用@csvin将其转换为 CSV jq:

    jq -r '.[] | [
            .id,
            .link,
            .metadata."@context",
            .metadata."@type",
            .metadata.name,
            .metadata.inLanguage,
            .metadata.image,
            .metadata.author[0]."@type",
            .metadata.author[0].name,
            .metadata.license."@type",
            .metadata.license.url,
            .metadata.license.name
    ] | @csv' file.json
    

    ...但请注意我是如何被迫决定我们只对第一作者感兴趣(.metadata.author子结构是一个数组)。

    输出:

    "4","https://pressbooks.online.ucf.edu/amnatgov/","http://schema.org","Book","American Government","en","https://pressbooks.online.ucf.edu/app/uploads/sites/4/2020/01/American-Government.png","Person","OpenStax","CreativeWork","https://creativecommons.org/licenses/by/4.0/","CC BY (Attribution)"
    

    要创建作为所有作者姓名串联的作者姓名字符串(对于作者类型也是如此),使用;分隔符,您可以代替.metadata.author[0].name上述使用[.metadata.author[].name]|join(";")(和 [.metadata.author[]."@type"]|join(";")类型),以便您的命令变为

    jq -r '.[] | [
            .id,
            .link,
            .metadata."@context",
            .metadata."@type",
            .metadata.name,
            .metadata.inLanguage,
            .metadata.image,
            ( [ .metadata.author[]."@type" ] | join(";") ),
            ( [ .metadata.author[].name    ] | join(";") ),
            .metadata.license."@type",
            .metadata.license.url,
            .metadata.license.name
    ] | @csv' file.json
    
    • 14
  2. aborruso
    2020-03-02T10:46:24+08:002020-03-02T10:46:24+08:00

    使用 Miller ( https://github.com/johnkerl/miller ) 你可以“扁平化” JSON,运行

    mlr --j2c cat input.json >output.csv
    
    +----+---------------------------------------------+-------------------+----------------+---------------------+---------------------+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------+---------------------------------------+-------------------------+------------------------+------------------------+--------------------------------+-------------------------------+------------------------+----------------------------------------------+-----------------------+-----------------------------------------------------+---------------------------------------------------------------------------+-----------------------------------------------------------------+
    | id | link                                        | metadata:@context | metadata:@type | metadata:name       | metadata:inLanguage | metadata:copyrightYear | metadata:disambiguatingDescription                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | metadata:image                                                                        | metadata:isBasedOn                    | metadata:author:0:@type | metadata:author:0:name | metadata:datePublished | metadata:copyrightHolder:@type | metadata:copyrightHolder:name | metadata:license:@type | metadata:license:url                         | metadata:license:name | _links:api:0:href                                   | _links:metadata:0:href                                                    | _links:self:0:href                                              |
    +----+---------------------------------------------+-------------------+----------------+---------------------+---------------------+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------+---------------------------------------+-------------------------+------------------------+------------------------+--------------------------------+-------------------------------+------------------------+----------------------------------------------+-----------------------+-----------------------------------------------------+---------------------------------------------------------------------------+-----------------------------------------------------------------+
    | 4  | https://pressbooks.online.ucf.edu/amnatgov/ | http://schema.org | Book           | American Government | en                  | 2016                   | The content of this textbook has been developed and arranged to provide a logical progression from the fundamental principles of institutional design at the founding, to avenues of political participation, to thorough coverage of the political structures that constitute American government. The book builds upon what students have already learned and emphasizes connections between topics as well as between theory and applications. The goal of each section is to enable students not just to recognize concepts, but to work with them in ways that will be useful in later courses, future careers, and as engaged citizens.  | https://pressbooks.online.ucf.edu/app/uploads/sites/4/2020/01/American-Government.png | https://ucf-dev.pb.unizin.org/pos2041 | Person                  | OpenStax               | 2016-01-06             | Organization                   | cnxamgov                      | CreativeWork           | https://creativecommons.org/licenses/by/4.0/ | CC BY (Attribution)   | https://pressbooks.online.ucf.edu/amnatgov/wp-json/ | https://pressbooks.online.ucf.edu/amnatgov/wp-json/pressbooks/v2/metadata | https://pressbooks.online.ucf.edu/wp-json/pressbooks/v2/books/4 |
    +----+---------------------------------------------+-------------------+----------------+---------------------+---------------------+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------+---------------------------------------+-------------------------+------------------------+------------------------+--------------------------------+-------------------------------+------------------------+----------------------------------------------+-----------------------+-----------------------------------------------------+---------------------------------------------------------------------------+-----------------------------------------------------------------+
    

    然后提取您想要的字段并使用重命名它们

    mlr -I --csv cut -f "id","link","metadata:@context","metadata:@type","metadata:name","metadata:inLanguage","metadata:image","metadata:author:0:@type","metadata:author:0:name","metadata:license:@type","metadata:license:url","metadata:license:name" \
    then label id,link,context,type,name,inLanguage,image,author_type,author_name,license_type,license_url,license_name output.csv
    

    输出将是

    +----+---------------------------------------------+-------------------+------+---------------------+------------+---------------------------------------------------------------------------------------+-------------+-------------+--------------+----------------------------------------------+---------------------+
    | id | link                                        | context           | type | name                | inLanguage | image                                                                                 | author_type | author_name | license_type | license_url                                  | license_name        |
    +----+---------------------------------------------+-------------------+------+---------------------+------------+---------------------------------------------------------------------------------------+-------------+-------------+--------------+----------------------------------------------+---------------------+
    | 4  | https://pressbooks.online.ucf.edu/amnatgov/ | http://schema.org | Book | American Government | en         | https://pressbooks.online.ucf.edu/app/uploads/sites/4/2020/01/American-Government.png | Person      | OpenStax    | CreativeWork | https://creativecommons.org/licenses/by/4.0/ | CC BY (Attribution) |
    +----+---------------------------------------------+-------------------+------+---------------------+------------+---------------------------------------------------------------------------------------+-------------+-------------+--------------+----------------------------------------------+---------------------+
    
    • 10

相关问题

  • 通过将前一行与当前和次要计算进行比较来转换现有的 .CSV

  • 如何将嵌入(引用)的 json 字符串转换为 json

  • 检查文本文件是否包含所有条目并且格式正确

  • jq 打印子对象中所有的键和值

  • jq 使用多个 --arg 添加或更新一个值

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve