繁体   English   中英

JOLT shift转换:按属性值过滤(不是名称)

[英]JOLT shift transformation: filter by the value of the property (not the name)

我正在尝试使用 Jolt 转换来转换 JSON,在此处寻找一些输入。 我正在尝试按属性的值进行过滤。

我的目标是获得一个仅包含具有“删除”操作的项目的数组。

这是我的输入和预期的 output:

输入

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "item": [
            {
              "id": "11_1_1_1",
              "action": "remove"
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "remove",
          "item": [
            {
              "id": "11_1_2_1",
              "action": "remove"
            }
          ]
        }
      ]
    }
  ]
}

预期 output

[
  {
    "id": "11_1_1_1",
    "action": "remove"
  },
  {
    "id": "11_1_2",
    "action": "remove"
  },
  {
    "id": "11_1_2_1",
    "action": "remove"
  }
]

你能帮我写一个简单的规范来做到这一点吗?

  • 让我们通过在第一个规范中遍历树时确定案例是"item"else case("*")来展平 JSON

  • 和 label 个人 arrays 由他们的action名称

  • 然后选择remove数组的对象最后一个规范

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "*": "@(1,id)[&1].&"
                }
              },
              "*": "j[&1].&"
            }
          },
          "*": "i[&1].&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(0,action)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "remove": {
        "*": ""
      }
    }
  }
]

jolt( http://jolt-demo.appspot.com/ )在操场上的演示是

在此处输入图像描述

您可以考虑另一个库Josson ,一个简单的语句就可以完成这项工作。 function 甚至支持未知和无限数量的路径级别。

https://github.com/octomix/josson

反序列化

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"id\": 11," +
    "  \"item\": [" +
    "    {" +
    "      \"id\": \"11_1\"," +
    "      \"action\": \"add\"," +
    "      \"item\": [" +
    "        {" +
    "          \"id\": \"11_1_1\"," +
    "          \"action\": \"add\"," +
    "          \"item\": [" +
    "            {" +
    "              \"id\": \"11_1_1_1\"," +
    "              \"action\": \"remove\"" +
    "            }" +
    "          ]" +
    "        }," +
    "        {" +
    "          \"id\": \"11_1_2\"," +
    "          \"action\": \"remove\"," +
    "          \"item\": [" +
    "            {" +
    "              \"id\": \"11_1_2_1\"," +
    "              \"action\": \"remove\"" +
    "            }" +
    "          ]" +
    "        }" +
    "      ]" +
    "    }" +
    "  ]" +
    "}");
    

转型

JsonNode node = josson.getNode(
    "cumulateCollect(item[action='remove']*.field(item:), item).flatten(1)");
System.out.println(node.toPrettyString());

Output

[ {
  "id" : "11_1_2",
  "action" : "remove"
}, {
  "id" : "11_1_1_1",
  "action" : "remove"
}, {
  "id" : "11_1_2_1",
  "action" : "remove"
} ]

暂无
暂无

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

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