【发布时间】:2023-03-31 18:46:01
【问题描述】:
我有一个 JSON 文件,我想使用 shell 脚本中的 jq 将其转换为 CSV 文件。我想从整个 JSON 文件中创建一行。我必须从价值中提取价值。行输出应该类似于
null,642,642,412,0,null,null
Here is my JSON file
{
"data": [
{
"name": "exits",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Exits",
"description": "Number of times someone exited the carousel"
},
{
"name": "impressions",
"period": "lifetime",
"values": [
{
"value": 642
}
],
"title": "Impressions",
"description": "Total number of times the media object has been seen"
},
{
"name": "reach",
"period": "lifetime",
"values": [
{
"value": 412
}
],
"title": "Reach",
"description": "Total number of unique accounts that have seen the media object"
},
{
"name": "replies",
"period": "lifetime",
"values": [
{
"value": 0
}
],
"title": "Replies",
"description": "Total number of replies to the carousel"
},
{
"name": "taps_forward",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Forward",
"description": "Total number of taps to see this story's next photo or video"
},
{
"name": "taps_back",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Back",
"description": "Total number of taps to see this story's previous photo or video"
}
]
}
您好尝试使用这个 jq 命令:
.data | map(.values[].value) | @csv
这给出了以下输出: jq:错误(在:70):对象({})在 csv 行中无效 退出状态 5
所以当我得到这个空的 JSON 对象时,它反映了一个错误。
请帮忙!!
行输出应该类似于
null,642,642,412,0,null,null
【问题讨论】: