【问题标题】:How to import nested json file into google sheets with some missing key:value pairs如何将嵌套的 json 文件导入谷歌表格,其中缺少一些键:值对
【发布时间】:2019-10-29 08:19:56
【问题描述】:

我有一个嵌套的 json 文件,如下所示:

{
  "id - 1": {
    "name": "ABC",
    "date": "1995-12",
    "capital": "4045",
    "URL": "https://www.example.com/",
  },
  "id - 2": {
    "name": "BCD",
    "date": "1962-08",
    "capital": "1,500",
    "total members": "51",
    "parent": "XVY Company",
    "URL": "https://www.example.org/",
  },
  "id -3 ": {
        .
        .
        .

  }
}

我想创建一个以键为标题的 csv/电子表格。问题是当某些 id 中缺少一些 key:value 对时,这些 id 存在于其他 id 中。

例如:parenttotal_members 不存在于 id-1 中,但存在于 id-2 中。

在这种情况下,我希望id-1 电子表格的单元格中有一个空单元格(或一些空指示符)。

name|date|capital|total-members| parent| url
ABC|1995-12|4045| null|null|https://www.example.com/
BCD|1962-08|1,500|51|XYZ company| https://www.example.org/

(我使用|分隔符,因为文本包含,

除了手动识别所有字段并遍历python3中的json文件之外,还有其他方法吗?

【问题讨论】:

    标签: json csv google-apps-script google-sheets


    【解决方案1】:

    不知道是不是你想要的回复。使用 jq (https://stedolan.github.io/jq/) 和 Miller (https://github.com/johnkerl/miller),从

    {
      "id - 1": {
        "name": "ABC",
        "date": "1995-12",
        "capital": "4045",
        "URL": "https://www.example.com/"
      },
      "id - 2": {
        "name": "BCD",
        "date": "1962-08",
        "capital": "1,500",
        "total members": "51",
        "parent": "XVY Company",
        "URL": "https://www.example.org/"
      }
    }
    

    正在运行

    <input.json jq '.[]' | mlr --j2c unsparsify
    

    你会有

    name,date,capital,URL,total members,parent
    ABC,1995-12,4045,https://www.example.com/,,
    BCD,1962-08,"1,500",https://www.example.org/,51,XVY Company
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多