通过 jq 使一个(子)JSON 对象出现在一行上
Posted
技术标签:
【中文标题】通过 jq 使一个(子)JSON 对象出现在一行上【英文标题】:Make one (sub-)JSON object appearing on one line by jq 【发布时间】:2017-05-11 08:55:37 【问题描述】:在 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
]
jq
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]
【问题讨论】:
【参考方案1】:这里的关键是“-c”选项,它实际上告诉 jq 使用 JSONLines 输出格式。
在您的特定情况下,解决方案很简单:
jq -c '.[]'
您的 shell 甚至可能允许您删除引号 :-)
【讨论】:
以上是关于通过 jq 使一个(子)JSON 对象出现在一行上的主要内容,如果未能解决你的问题,请参考以下文章