如何从嵌套的地图中提取数据?

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

Jq: how to extract from map of maps?

问题

Sure, here is the translated content you requested:

我有

{
    "version": 2,
    "clients": {
        "c1": {
            "updated": "0001-01-01T00:00:00Z",
            "instances": {
                "i1": {
                    "updated": "2023-03-19T17:45:03.703984279+01:00",
                    "chargePower": 349.9999940395355
                }
            }
        },
        "c2": {
            "updated": "0001-01-01T00:00:00Z",
            "instances": {
                "i2": {
                    "updated": "2023-02-10T18:34:56.352428175+01:00",
                    "chargePower": 0
                }
            }
        }
    }
}

并且想要一个实例 ID(i1...)和它所关联的映射,如下所示:

{
    "i1": {
        "updated": "2023-03-19T17:45:03.703984279+01:00",
        "chargePower": 349.9999940395355
    },
    "i2": {
        "updated": "2023-02-10T18:34:56.352428175+01:00",
        "chargePower": 0
    }
}

I hope this helps with your translation needs.

英文:

I have

{
    "version": 2,
    "clients": {
        "c1": {
            "updated": "0001-01-01T00:00:00Z",
            "instances": {
                "i1": {
                    "updated": "2023-03-19T17:45:03.703984279+01:00",
                    "chargePower": 349.9999940395355
                }
            }
        },
        "c2": {
            "updated": "0001-01-01T00:00:00Z",
            "instances": {
                "i2": {
                    "updated": "2023-02-10T18:34:56.352428175+01:00",
                    "chargePower": 0
                }
            }
        }
    }
}

and would like a map of instance id (i1...) and the map it has associated like this:

{
    "i1": {
        "updated": "2023-03-19T17:45:03.703984279+01:00",
        "chargePower": 349.9999940395355
    },
    "i2": {
        "updated": "2023-02-10T18:34:56.352428175+01:00",
        "chargePower": 0
    }
}

I get as far as

.clients | to_entries | .[].value.instances 

which gives

{
  "i1": {
    "updated": "2023-03-19T17:45:03.703984279+01:00",
    "chargePower": 349.9999940395355
  }
}
{
  "i2": {
    "updated": "2023-02-10T18:34:56.352428175+01:00",
    "chargePower": 0
  }
}

but fail to convert that back to a map. Here's a jqplay: https://jqplay.org/s/FMnrrUIV27U

答案1

得分: 1

将“key”的“value”更新为“instances”的“first”值

{
  "c1": {
    "updated": "2023-03-19T17:45:03.703984279+01:00",
    "chargePower": 349.9999940395355
  },
  "c2": {
    "updated": "2023-02-10T18:34:56.352428175+01:00",
    "chargePower": 0
  }
}
英文:
.clients | with_entries(.value = (.value.instances | to_entries[0].value))

Will update the value of the key with the first value of instances

{
  "c1": {
    "updated": "2023-03-19T17:45:03.703984279+01:00",
    "chargePower": 349.9999940395355
  },
  "c2": {
    "updated": "2023-02-10T18:34:56.352428175+01:00",
    "chargePower": 0
  }
}

答案2

得分: 1

以下是翻译好的部分:

"不确定我是否理解您的问题(缺少预期的输出),但如果您要查找具有实例键作为键的对象,那么您可以在父对象上使用 map,并在嵌套对象的结果数组上使用 add:"

"或者可以使用基于 reduce 的方法:"

"输出:"

{
  "i1": {
    "updated": "2023-03-19T17:45:03.703984279+01:00",
    "chargePower": 349.9999940395355
  },
  "i2": {
    "updated": "2023-02-10T18:34:56.352428175+01:00",
    "chargePower": 0
  }
}
英文:

Not sure if I got your question right (it is lacking the expected output), but if you are looking for an object with the instances' keys as keys, then you could use map on the parent objects and add on the resulting array of nested objects:

.clients | map(.instances) | add

Or a reduce-based approach:

reduce .clients[].instances as $i ({}; . + $i)

Output:

{
  "i1": {
    "updated": "2023-03-19T17:45:03.703984279+01:00",
    "chargePower": 349.9999940395355
  },
  "i2": {
    "updated": "2023-02-10T18:34:56.352428175+01:00",
    "chargePower": 0
  }
}

huangapple
  • 本文由 发表于 2023年3月21日 02:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794207.html
匿名

发表评论

匿名网友

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

确定