我写Jolt是为了转换这些数据,但没有得到想要的结果

如果practice_locprac_numtopId对于两个或更多个数据是相同的,则它们将与subList内的单独S1S2组合在一起.否则,它们将按原样传递,仅添加SubList.

Data

[
  {
    "practice_loc": "120",
    "prac_num": "oswal",
    "topId": "t1",
    "S1": "A1",
    "S2": "B1"
  },
  {
    "practice_loc": "120",
    "prac_num": "oswal",
    "topId": "t1",
    "S1": "A2",
    "S2": ""
  },
  {
    "practice_loc": "334",
    "prac_num": "L3",
    "topId": "plumcherry",
    "S1": "A3",
    "S2": ""
  },
  {
    "practice_loc": "987",
    "prac_num": "L3",
    "topId": "artica",
    "S1": "A5",
    "S2": "B7"
  }
]

Expected Output:

[
  {
    "practice_loc": "120",
    "prac_num": "oswal",
    "topId": "t1"
    "subList": [
      {
        "S1": "A1",
        "S2": "B1"
      },
      {
        "S1": "A2",
        "S2": ""
      }
    ]
  },
  {
    "practice_loc": "334",
    "prac_num": "L3",
    "topId": "plumcherry"
    "subList": [
      {
        "SubID1": "A3",
        "SubID2": ""
      }
    ]
  },
  {
    "practice_loc": "987",
    "prac_num": "L3",
    "topId": "artica",
    "subList": [
      {
        "SubID1": "A5",
        "SubID2": "B7"
      }
    ]
  }
]

这是我试过的,但没有得到想要的结果它没有打印任何东西

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@(1,practice_loc).@(1,prac_num).@(1,topId)"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "MANY"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "practice_loc": "[#4].&",
            "prac_num": "[#4].&",
            "topId": "[#4].&",
            "S*": "[#4].subList[&1].&"
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "practice_loc": "ONE",
        "prac_num": "ONE",
        "topId": "ONE"
      }
    }
  }
]

推荐答案

你方目前的规格相当不错.会适合这样重新安排

[
  { // group by those three attributes
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,practice_loc).@(1,prac_num).@(1,topId).&",
        "S*": "@(1,practice_loc).@(1,prac_num).@(1,topId).subList[&1].&"
      }
    }
  },
  { // get rid of wrappers
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": ""
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE", // pick only single one from repeating components 
        "subList": "MANY"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE", // pick only single one from repeating components 
        "subList": "MANY"
      }
    }
  },
  { // get rid of generated nulls within subList arrays 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

Edit for illustration:下面,我粘贴了我在切换Configure部分的ADVANCED选项卡后获得的图像,用于JoltTransformJSON处理器,其版本为1.21.0,与NiFi一样.顺便说一句,你的也是最新的版本.

enter image description here

Json相关问答推荐

在PowerShell中,如何获取数据对所在的JSON对象的名称

Allof Indide的JSON模式之一

如何判断响应数组是否存在以及S是否有其他内容...?

JSON API返回多个数组,需要帮助拼合数据以存储在SQL Server数据库表中

解析Ansible AWS ec2_security_group中的json以提取安全组ID

jq:将一个数组与多个数组连接起来

JOLT分裂和数组数据

从 json 对象中过滤掉所有出现的给定属性

如何使用 LINQ 在 C# 中重构对象?

缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter

如何使用 gson 将数据保存在 json 文件中?

如何在 Go 中生成带有排序键的 JSON?

JSON对象中的JavaScript递归搜索

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

在 Jersey 服务中使用 JSON 对象

如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?

如何向从文件中检索的 JSON 数据添加键值?

我可以使用空字符串作为对象标识符吗?

如何将 MongoDB 查询转换为 JSON?

无法将空值放入 JSON 对象