将嵌套字典合并到根 jq
Merge nested dict to root jq
我正在尝试创建类似 table 的东西,其中包含实例 ID 和他们的标签以及其他详细信息,而我卡住的那一步是移动作为 Dict 对象放置在嵌套数组中的 EC2 标签。这里已经转换了“Tags”字典,只需要将它与其他数据放在一起即可。
{
"Id": "i-0e27418e091ea97a7",
"Name": "php"
}
{
"Id": "i-0b0a0cb6223701cb5",
"Name": "Logics-SF-Migration",
"q": "ngix"
}
上面是期望的结果,下面是来源 json。
{
"Id": "i-0e27418e091ea97a7",
"Tags": {
"Name": "php"
}
}
{
"Id": "i-0b0a0cb6223701cb5",
"Tags": {
"Name": "Main,
"q": "ngix"
}
}
只需添加带有.Tags
的根路径,然后将其删除。您也可以使用临时变量,即 .Tags as $v | del(.Tags) | . + $v
jq '. + .Tags | del(.Tags)'
最简单的解决方案是
jq 'del(.Tags) + .Tags'
{
"Id": "i-0e27418e091ea97a7",
"Name": "php"
}
{
"Id": "i-0b0a0cb6223701cb5",
"Name": "Main",
"q": "ngix"
}
与 . + .Tags | del(.Tags)
等其他解决方案的主要区别在于,这会从未更改的源中删除,而不是从填充的结果中删除,这确实会影响 .Tags.Tags
中的内容(如果存在)。示例:
{
"Id": "123",
"Tags": {
"Name": "What about nested Tags?",
"Tags": "Should they be deleted?"
}
}
jq '( . + .Tags | del(.Tags) ),
( del(.Tags) + .Tags )'
{
"Id": "123",
"Name": "What about nested Tags?"
}
{
"Id": "123",
"Name": "What about nested Tags?",
"Tags": "Should they be deleted?"
}
我正在尝试创建类似 table 的东西,其中包含实例 ID 和他们的标签以及其他详细信息,而我卡住的那一步是移动作为 Dict 对象放置在嵌套数组中的 EC2 标签。这里已经转换了“Tags”字典,只需要将它与其他数据放在一起即可。
{
"Id": "i-0e27418e091ea97a7",
"Name": "php"
}
{
"Id": "i-0b0a0cb6223701cb5",
"Name": "Logics-SF-Migration",
"q": "ngix"
}
上面是期望的结果,下面是来源 json。
{
"Id": "i-0e27418e091ea97a7",
"Tags": {
"Name": "php"
}
}
{
"Id": "i-0b0a0cb6223701cb5",
"Tags": {
"Name": "Main,
"q": "ngix"
}
}
只需添加带有.Tags
的根路径,然后将其删除。您也可以使用临时变量,即 .Tags as $v | del(.Tags) | . + $v
jq '. + .Tags | del(.Tags)'
最简单的解决方案是
jq 'del(.Tags) + .Tags'
{
"Id": "i-0e27418e091ea97a7",
"Name": "php"
}
{
"Id": "i-0b0a0cb6223701cb5",
"Name": "Main",
"q": "ngix"
}
与 . + .Tags | del(.Tags)
等其他解决方案的主要区别在于,这会从未更改的源中删除,而不是从填充的结果中删除,这确实会影响 .Tags.Tags
中的内容(如果存在)。示例:
{
"Id": "123",
"Tags": {
"Name": "What about nested Tags?",
"Tags": "Should they be deleted?"
}
}
jq '( . + .Tags | del(.Tags) ),
( del(.Tags) + .Tags )'
{
"Id": "123",
"Name": "What about nested Tags?"
}
{
"Id": "123",
"Name": "What about nested Tags?",
"Tags": "Should they be deleted?"
}