【问题标题】:Extract field name into new object using JQ使用 JQ 将字段名称提取到新对象中
【发布时间】:2021-05-27 12:34:51
【问题描述】:

Spring 在 env-endpoint 提供有关环境的信息。我想简化输出,看起来像这样:

{
  "propertySources": [
    {
      "name": "ports",
      "properties": {
        "server.port": {
          "value": 9110
        },
        "management.port": {
          "value": 9350
        }
      }
    },
    {
      "name": "trees",
      "properties": {
        "oak": {
          "value": "true"
        }
      }
    },
    {
      "name": "elephants",
      "properties": {}
    },
    ...
}

我只想看到键值对作为输出,如下所示:

{
  "server.port": 9110,
  "management.port": 9350,  
  "oak": "true",
  ...
}

使用 JQ 我尝试了一些事情,开始于

curl https://localhost:9350/actuator/env/ -s | jq '[.propertySources[].properties[]]'

并尝试mapselectwith_entries,但我无法让它运行。 你能给我一个正确的方向吗?

【问题讨论】:

    标签: json jq


    【解决方案1】:

    您可以简单地添加 properties 对象,但为了获得该输出作为回报,您还需要取出 value 字段。

    .propertySources | map(.properties) | add | map_values(.value)
    

    Online demo

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 2019-08-25
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2021-06-24
      • 2017-06-22
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多