json - 通过 jq 使一个(子)JSON 对象出现在一行上

标签 json google-bigquery jq reformatting jsonlines

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

One JSON object, including any nested/repeated fields, must appear on each line.

refer to: 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
      }
    ]
  }
}

如何通过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}]}

最佳答案

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

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

jq -c '.[]'

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

关于json - 通过 jq 使一个(子)JSON 对象出现在一行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910858/

相关文章:

python - Django 测试时未找到 Postgres 函数 json_array_elements

java - 该列表的格式是什么?

android - 如何从 firebase 导出无崩溃用户?

json - 使用 jq 从 json 流中进行第一次匹配

json - BASH JQ 命令

c# - 如何使用 .Net 编写漂亮格式的 JSON 文件

java - JSON 字段显示不应基于 View

sql - BigQuery - 复杂的相关查询

google-bigquery - BigQuery:将多个字段汇总到数组中

json - 使用 jq 计算缺少键的记录