JOLT - 在嵌套数组中加入数组 [英] JOLT - Join arrays in nested array

查看:38
本文介绍了JOLT - 在嵌套数组中加入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下转变.但是,我的解决方案将不需要的空值添加到最终数组中.

I am trying to achieve the following transformation. However, my solution adds undesired null values into the final array.

转换需要为所有 root 元素在 child 数组中移动名称.我创建了 3 个案例来说明问题.

The transformation needs to shift names in child array for all root elements. I have created 3 cases to illustrate the problem.

{
  "root": [
    {
      "child": [
        {
          "name": "John"
        },
        {
          "name": "Frazer"
        }
      ]
    },
    {
      "child": [
        {
          "name": "Brandon"
        },
        {
          "name": "Josef"
        }
      ]
    }
  ]
}

期望输出

{
  "NAMES": ["John,Frazer","Brandon,Josef"]
}

情况 2:一个 child 为空

输入

{
  "root": [
    {
      "child": []
    },
    {
      "child": [
        {
          "name": "Brandon"
        },
        {
          "name": "Josef"
        }
      ]
    }
  ]
}

期望输出

{
   "NAMES": ["","Brandon,Josef"]
}

案例3:所有child都是空的

输入

{
  "root": [
    {
      "child": []
    },
    {
      "child": []
    }
  ]
}

期望输出

{
   "NAMES": ["",""]
}

root 数组总是至少有 1 个元素.

root array will always have at least 1 element.

当前的 JOLT 规范工作正常,除了 child 是空数组的情况.它生成 null 值,而我试图指定一个 空字符串(或任何硬编码的字符串值,例如 "NO_NAMES")

Current JOLT spec works fine except for cases where child is an empty array. It generates null values and I'm trying to specify an empty string instead (or any hardcoded string value such as "NO_NAMES")

[
  {
    "operation": "shift",
    "spec": {
      "root": {
        "*": {
          "child": {
            "*": {
              "name": "NAMES[&3]"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "NAMES": {
        "*": "=trim"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "NAMES": "MANY"
    }
    },
  {
    "operation": "default",
    "spec": {
      "NAMES": []
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "NAMES": {
        "*": "=join(',',@0)"
      }
    }
  }
]

推荐答案

您可以应用连续转换 modify-overwrite-beta 然后 shift 以确定逗号-列表的分隔元素(除非它们的大小为零,这种情况下只会出现双引号),然后将它们连接到一个列表中,例如

You can apply consecutive transformations modify-overwrite-beta and then shift in order to determine comma-seperated elements of the list(unless they have zero size,this case only double quotes will appear), and then concatenate them within a list such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "root": {
        "*": {
          "child": { "*": "@(0,name)" },
          "NAMES": "=join(',',@(1,child))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "root": {
        "*": {
          "NAMES": "&"
        }
      }
    }
  }
]

这篇关于JOLT - 在嵌套数组中加入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆