繁体   English   中英

颠簸变换JSON数组保持rest的字段

[英]Jolt transform JSON array keep rest of the fields

如何在 Jolt 变换 JSON 数组中保留其他字段,我正在尝试使用通配符但最终 output 中未添加字段?

这是我正在使用的示例输入

[
  {
    "foundduring": "D-DC",
    "user_type": "type1",
    "location": "location1"
  },
  {
    "foundduring": "D-DG",
    "user_type": "type2",
    "location": "location2"
  },
  {
    "foundduring": "D-DI",
    "user_type": "type3",
    "location": "location3"
  }
]

我正在使用以下 Jolt 转换并尝试使用通配符:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "foundduring": {
          "D-DC": {
            "#CycleCount": "[&3].foundduring"
          },
          "D-DG": {
            "#Pick": "[&3].foundduring"
          },
          "D-DI": {
            "#Issue": "[&3].foundduring"
          }
        },
        "@": "&"
      }
    }
  }
]

以下是我预期的 output 发生移位操作,然后需要保留所有其他字段

[
  {
    "foundduring" : "CycleCount",
    "user_type" : "type1",
    "location" : "location1"
  },
   {
    "foundduring" : "Pick",
    "user_type" : "type2",
    "location" : "location2"
  },
   {
    "foundduring" : "Issue",
    "user_type" : "type3",
    "location" : "location3"
  }
]

实际 Output 来了:

[
  {
    "foundduring": "CycleCount"
  },
  {
    "foundduring": "Pick"
  },
  {
    "foundduring": "Issue"
  }
]

考虑使用"*"通配符代替"@" ,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "foundduring": {
          "D-DC": {
            "#CycleCount": "[&3].&2"
          },
          "D-DG": {
            "#Pick": "[&3].&2"
          },
          "D-DI": {
            "#Issue": "[&3].&2"
          }
        },
        "*": "[&1].&"
      }
    }
  }
]

顺便说一句,无需获取键名"foundduring" ,只需使用&2替换 go 2 从当前分支升级并获取该值。

站点http://jolt-demo.appspot.com/上的演示

在此处输入图像描述

您可以考虑另一个图书馆Josson

https://github.com/octomix/josson

反序列化

Josson josson = Josson.fromJsonString(
    "[" +
    "  {" +
    "    \"foundduring\": \"D-DC\"," +
    "    \"user_type\": \"type1\"," +
    "    \"location\": \"location1\"" +
    "  }," +
    "  {" +
    "    \"foundduring\": \"D-DG\"," +
    "    \"user_type\": \"type2\"," +
    "    \"location\": \"location2\"" +
    "  }," +
    "  {" +
    "    \"foundduring\": \"D-DI\"," +
    "    \"user_type\": \"type3\"," +
    "    \"location\": \"location3\"" +
    "  }" +
    "]");
    

转型

JsonNode node = josson.getNode(
    "field(foundduring.caseValue('D-DC','CycleCount','D-DG','Pick','D-DI','Issue'))");
System.out.println(node.toPrettyString());

Output

[ {
  "foundduring" : "CycleCount",
  "user_type" : "type1",
  "location" : "location1"
}, {
  "foundduring" : "Pick",
  "user_type" : "type2",
  "location" : "location2"
}, {
  "foundduring" : "Issue",
  "user_type" : "type3",
  "location" : "location3"
} ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM