【问题标题】:What kind of JSON JOLT Spec to get key-value output where key is a data value and value is an array什么样的 JSON JOLT Spec 来获取键值输出,其中键是数据值,值是数组
【发布时间】:2022-01-17 22:24:47
【问题描述】:

我正在尝试找到一个能产生所需输出的规范数组

输入:

{
  "aggregations": {
    "masterId": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "1Q52",
          "doc_count": 3,
          "serialNumbers": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "3R24Z3",
                "count": 1
              },
              {
                "key": "526GA2",
                "count": 1
              },
              {
                "key": "873XHE",
                "count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

规格: 试图解决这个问题

期望的输出:

{
  "1Q52": ["3R24Z3", "526GA2", "873XHE"]
}

我当前的 Spec 数组是

[
  {
    "operation": "shift",
    "spec": {
      "aggregations": {
        "masterId": {
          "buckets": {
            "*": {
              "key": "key",
              "serialNumbers": {
                "buckets": {
                  "*": {
                    "key": "key"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

我目前的输出是

{
  "key" : [ "1Q52", "3R24Z3", "526GA2", "873XHE" ]
}

什么样的规格数组可以给我想要的输出?

【问题讨论】:

    标签: arrays json key jolt


    【解决方案1】:

    您可以通过使用"key": "@(4,key)" 等方式提升4级并获取作为数组键的值

    [
      {
        "operation": "shift",
        "spec": {
          "aggregations": {
            "masterId": {
              "buckets": {
                "*": {
                  "serialNumbers": {
                    "buckets": {
                      "*": {
                        "key": "@(4,key)"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    其中 : 和三倍 { 字符遍历被计数以达到所需的键值。

    【讨论】:

    • 嗨@AmrutKulkarni。您可以查看my former answersJolt's releasesbaazarvoice/jolt。祝你工作愉快。
    • 成功了!谢谢 介意解释这背后的机制吗?我对 JOLT 很陌生。我很难找到关于如何学习它的好资源,但它在我的工作中经常使用。是否有任何好的资源来解释 JOLT,尤其是如何使用 #、$ 和 @ 以及如何从 JSON 文档的部分中获取数据,就像您的规范数组所做的那样
    • 谢谢!我会检查一下我希望能更好地理解 Jolt,因为它经常用于格式化从我们的数据库中提取的数据,以便我们的中间件可以使用它
    猜你喜欢
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多