如何使用jq将这个对象转换为这个对象?

huangapple go评论73阅读模式
英文:

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&#39;m trying to convert the object below using jq:

```json
{
    &quot;locals&quot;: {
      &quot;products&quot;: {
        &quot;core-tower1&quot;: {
          &quot;core-tower1-product-a&quot;: [&quot;dev&quot;],
          &quot;core-tower1-product-b&quot;: [&quot;dev&quot;, &quot;prod&quot;]
        },
        &quot;core-tower2&quot;: {
          &quot;core-tower2-product-a&quot;: [&quot;pol&quot;, &quot;onb&quot;],
          &quot;core-tower2-product-a&quot;: [&quot;dev&quot;, &quot;prod&quot;]
        }
      }
    }
  }

to this

{
        &quot;core-tower1&quot;: [
          &quot;core-tower1-product-a&quot;,
          &quot;core-tower1-product-b&quot;
        ]
        &quot;core-tower2&quot;: [
          &quot;core-tower2-product-a&quot;,
          &quot;core-tower2-product-a&quot;
        ]
      }

I tried this it as seen below:

jq {&#39;(.locals.products|keys[])&#39;:&#39;.locals.products|.[]|keys&#39;}

but I got the result below:

 {
        &quot;core-tower1&quot;: [
          &quot;core-tower1-product-a&quot;,
          &quot;core-tower1-product-b&quot;
        ]
        &quot;core-tower1&quot;: [
          &quot;core-tower2-product-a&quot;,
          &quot;core-tower2-product-a&quot;
        ]
      }
 {
        &quot;core-tower2&quot;: [
          &quot;core-tower1-product-a&quot;,
          &quot;core-tower1-product-b&quot;
        ]
        &quot;core-tower2&quot;: [
          &quot;core-tower2-product-a&quot;,
          &quot;core-tower2-product-a&quot;
        ]
      }

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 &#39;
  reduce (inputs | select(has(1))[0][2:]) as $p ({};
    setpath($p | .[-2:] |= [last]; $p[-2])
  )
&#39;
{
  &quot;core-tower1&quot;: [
    &quot;core-tower1-product-b&quot;,
    &quot;core-tower1-product-b&quot;
  ],
  &quot;core-tower2&quot;: [
    &quot;core-tower2-product-a&quot;,
    &quot;core-tower2-product-a&quot;
  ]
}

答案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 &#39;
    .locals.products | to_entries
    | reduce .[] as $e (
        {}; . + { ($e.key): $e.value | keys }
    )
&#39;

Notice that both keys in core-tower2 are identical, which leads to jq dropping the duplicate, resulting in

{
  &quot;core-tower1&quot;: [
    &quot;core-tower1-product-a&quot;,
    &quot;core-tower1-product-b&quot;
  ],
  &quot;core-tower2&quot;: [
    &quot;core-tower2-product-a&quot;
  ]
}

huangapple
  • 本文由 发表于 2023年7月18日 12:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定