json - JQ add properties to nested object in nested array
问题描述
I have the following json:
{
"first": {
"second" : "A"
},
"array": [
{
"name" : "AAA",
"something": {
"hola": "hi"
}
},
{
"name" : "BBB",
"something": {
"hola": "hi"
}
}
]
}
I would like to trasform it adding a property to the something
object, using the value from the name
property of the parent, like:
I have the following json:
{
"first": {
"second" : "A"
},
"array": [
{
"name" : "AAA",
"something": {
"hola": "hi",
"NEW_PROPERTY": "AAA"
}
},
{
"name" : "BBB",
"something": {
"hola": "hi",
"NEW_PROPERTY": "BBB"
}
}
]
}
Which jq expression can do this?
解决方案
Try this jq script:
<file jq '.array = [ .array[] | .something.NEW_PROPERTY = .name ]'
This is replacing the array
by another one that is the same as the original one with one more key NEW_PROPERTY
.
推荐阅读
- java - 如何解决崩溃的应用程序问题和 FATAL EXCEPTION 错误?
- java - 同一个 .java 文件中的多条记录
- python - 到处使用 `from __future__ import annotations` 有什么缺点吗?
- powershell - 如果使用调用命令,本地脚本输出将保存在远程服务器上
- php - 如何转换 laravel 查询
- python - 模块中的连续错误:TypeError:Artist() 不接受任何参数
- php - 为什么for循环后的mycode没有执行?
- geometry - 使用 S2 几何库在数据库上执行位置邻近搜索
- excel - vb.net 在等待进程时显示动画 Gif
- python - 退格字符不起作用 - 使用 python 在 cmd 上打印