2

在 Cloud BigQuery 中,接受的 JSON 格式为:

一个 JSON 对象,包括任何嵌套/重复的字段,必须出现在每一行上。

参考:https ://cloud.google.com/bigquery/data-formats#json_format

现在,给定一个json:

{
  "1": {
    "kind": "person",
    "fullName": "John Doe",
    "age": 22,
    "gender": "Male",
    "citiesLived": [
      {
        "place": "Seattle",
        "numberOfYears": 5
      },
      {
        "place": "Stockholm",
        "numberOfYears": 6
      }
    ]
  },
  "2": {
    "kind": "person",
    "fullName": "Jane Austen",
    "age": 24,
    "gender": "Female",
    "citiesLived": [
      {
        "place": "Los Angeles",
        "numberOfYears": 2
      },
      {
        "place": "Tokyo",
        "numberOfYears": 2
      }
    ]
  }
}

如何将其转换为以下 by jq

{"kind": "person", "fullName": "John Doe", "age": 22, "gender": "Male", "citiesLived": [{ "place": "Seattle", "numberOfYears": 5}, {"place": "Stockholm", "numberOfYears": 6}]}
{"kind": "person", "fullName": "Jane Austen", "age": 24, "gender": "Female", "citiesLived": [{"place": "Los Angeles", "numberOfYears": 2}, {"place": "Tokyo", "numberOfYears": 2}]}
4

1 回答 1

3

这里的关键是“-c”选项,它实际上告诉 jq 使用 JSONLines 输出格式。

在您的特定情况下,解决方案很简单:

jq -c '.[]'

您的 shell 甚至可能允许您删除引号 :-)

于 2017-05-11T09:04:09.547 回答