通过jq使一个(sub-)JSON对象出现在一行
Make one (sub-)JSON object appearing on one line by jq
在 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 甚至可能允许您删除引号 :-)
在 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 甚至可能允许您删除引号 :-)