json - 使用 jq 在现有 JSON 中添加相同的数组元素
问题描述
我有一个 json 文件,我想在 json 的另一个位置从顶部添加一些值。我正在尝试使用 jq 命令行。
{
"channel": "mychannel",
"videos": [
{
"id": "10",
"url": "youtube.com"
},
{
"id": "20",
"url": "youtube.com"
}
]
}
输出将是:
{
"channel": "mychannel",
"videos": [
{
"channel": "mychannel",
"id": "10",
"url": "youtube.com"
},
{
"channel": "mychannel",
"id": "20",
"url": "youtube.com"
}
]
}
在我的 json 中,“通道”是静态的,总是相同的值。我需要一种在每个视频数组中始终连接的方法。
有人可以帮助我吗?
jq .videos + 频道
解决方案
Use a variable to remember .channel
in the later stages of the pipeline.
$ jq '.channel as $ch | .videos[].channel = $ch' tmp.json
{
"channel": "mychannel",
"videos": [
{
"id": "10",
"url": "youtube.com",
"channel": "mychannel"
},
{
"id": "20",
"url": "youtube.com",
"channel": "mychannel"
}
]
}
推荐阅读
- android - Flutter build_runner 包不会更新
- elixir - 在 Ecto 查询中使用变量来引用命名绑定
- powershell - Update-Module 未在脚本中将 PSGallery 存储库作为计划任务找到
- python - 如何将字符串评估为变量名
- imageview - 在 ARCore 中显示来自网络的图像
- excel - 删除重复项,在不同列中保持最低值
- c - 为什么提供 1 个元素后 C 程序停止执行?
- r - Any pattern of same length that isn't the pattern desired
- javascript - 计算输入字段的总数并显示每个输入字段的百分比
- java - Write big files using RandomAccessFile class