如何提取满足条件的部分aeson值?

时间:2017-08-17 13:25:36

标签: json haskell filter lens aeson

我有一个JSON结构,如下所示:

{
  "instances": [
    {
      "instanceId": "i-1234",
      "tags": [
        {
          "value": "author1useast1",
          "key": "hostname"
        }
      ]
    },
    {
      "instanceId": "i-5678",
      "tags": [
        {
          "value": "proxy1useast1",
          "key": "hostname"
        }
      ]
    }
  ]
}

我想获得instances/instanceId所有instances/tags hostname author1useast1 {。}}的所有key "instances" . _Values列表。

我考虑首先使用instanceId获取实例列表,然后将其映射到(tags InnerClass innerObj = new OuterClass().new InnerClass(); )元组列表中,然后进行过滤。然而,这对我来说效率非常低。

这样做有更优雅/惯用的方法吗?

非常感谢!

1 个答案:

答案 0 :(得分:2)

如果您想使用镜头,可以使用filtered光学镜片,例如:

  key "instances" . values
. filtered (anyOf (key "tags" . values) $
    allOf (key "key") (=="hostname")
    <&&> allOf (key "value") (=="author1useast1"))
. key "instanceId"