英文:
How do I use jq to convert this object to this object?
问题
I'm trying to convert the object below using jq:
{
"locals": {
"products": {
"core-tower1": {
"core-tower1-product-a": ["dev"],
"core-tower1-product-b": ["dev", "prod"]
},
"core-tower2": {
"core-tower2-product-a": ["pol", "onb"],
"core-tower2-product-a": ["dev", "prod"]
}
}
}
}
to this
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
]
"core-tower2": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
I tried this it as seen below:
jq {'(.locals.products|keys[])':'.locals.products|.[]|keys'}
but I got the result below:
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
]
"core-tower1": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
{
"core-tower2": [
"core-tower1-product-a",
"core-tower1-product-b"
<details>
<summary>英文:</summary>
I'm trying to convert the object below using jq:
```json
{
"locals": {
"products": {
"core-tower1": {
"core-tower1-product-a": ["dev"],
"core-tower1-product-b": ["dev", "prod"]
},
"core-tower2": {
"core-tower2-product-a": ["pol", "onb"],
"core-tower2-product-a": ["dev", "prod"]
}
}
}
}
to this
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
]
"core-tower2": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
I tried this it as seen below:
jq {'(.locals.products|keys[])':'.locals.products|.[]|keys'}
but I got the result below:
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
]
"core-tower1": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
{
"core-tower2": [
"core-tower1-product-a",
"core-tower1-product-b"
]
"core-tower2": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
Let me know if there's a different way to do this or is jq the best approach here.
答案1
得分: 1
If your issue is with the swallowed duplicate keys, then consider using the --stream
option which provides the input document "in pieces", so that the parts can be processed before any duplicate keys can collapse. This approach, however, may need more insight into the logical structuring of your document, so let me assume here that you want to generally cut off the first two levels .[2:]
, build the values array from the level directly above the scalars/leaves (i.e. the second-to-last level, -2
), and just copy over all levels in between:
jq --stream -n '
reduce (inputs | select(has(1))[0][2:]) as $p ({};
setpath($p | .[-2:] |= [last]; $p[-2])
)
'
{
"core-tower1": [
"core-tower1-product-b",
"core-tower1-product-b"
],
"core-tower2": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
英文:
If your issue is with the swallowed duplicate keys, then consider using the --stream
option which provides the input document "in pieces", so that the parts can be processed before any duplicate keys can collapse. This approach, however, may need more insight into the logical structuring of your document, so let me assume here that you want to generally cut off the first two levels .[2:]
, build the values array from the level directly above the scalars/leaves (i.e. the second-to-last level, -2
), and just copy over all levels in between:
jq --stream -n '
reduce (inputs | select(has(1))[0][2:]) as $p ({};
setpath($p | .[-2:] |= [last]; $p[-2])
)
'
{
"core-tower1": [
"core-tower1-product-b",
"core-tower1-product-b"
],
"core-tower2": [
"core-tower2-product-a",
"core-tower2-product-a"
]
}
答案2
得分: 0
以下是使用reduce
的解决方案:
jq '
.locals.products | to_entries
| reduce .[] as $e (
{}; . + { ($e.key): $e.value | keys }
)
'
请注意,core-tower2
中的两个键是相同的,这导致jq删除了重复的键,结果如下:
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
],
"core-tower2": [
"core-tower2-product-a"
]
}
英文:
Here's a solution with reduce
:
jq '
.locals.products | to_entries
| reduce .[] as $e (
{}; . + { ($e.key): $e.value | keys }
)
'
Notice that both keys in core-tower2
are identical, which leads to jq dropping the duplicate, resulting in
{
"core-tower1": [
"core-tower1-product-a",
"core-tower1-product-b"
],
"core-tower2": [
"core-tower2-product-a"
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论