繁体   English   中英

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

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

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

我的目标是获得一个仅包含 typeName 为“xx”的项目的数组。

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

输入

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "type": {
        "id": "11_1_xx",
        "typeName": "xx"
      },
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "type": {
            "id": "11_1_1_zz",
            "typeName": "zz"
          },
          "item": [
            {
              "id": "11_1_1_1",
              "action": "add",
              "type": {
                "id": "11_1_1_1_xx",
                "typeName": "xx"
              }
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "add",
          "type": {
            "id": "11_1_2_xx",
            "typeName": "xx"
          },
          "item": [
            {
              "id": "11_1_2_1",
              "action": "add",
              "type": {
                "id": "11_1_2_1_zz",
                "typeName": "zz"
              }
            }
          ]
        }
      ]
    }
  ]
}

预期 output

[
  {
    "id": "11_1",
    "action": "add",
    "type": {
      "id": "11_1_xx",
      "typeName": "xx"
    }
  },
  {
    "id": "11_1_1_1",
    "action": "add",
    "type": {
      "id": "11_1_1_1_xx",
      "typeName": "xx"
    }
}, {
    "id": "11_1_2",
    "action": "add",
    "type": {
      "id": "11_1_2_xx",
      "typeName": "xx"
    }
  }
]


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

需要通过第一个转换规范中的idtype.typeName值来分隔对象,以便在下一个规范中通过type.typeName=xx过滤掉通过使用@(1,id).@(1,typeName)明确选择的id@(1,id).@(1,typeName)右侧的叶元素标识符,例如

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "type": {
                    "@(1,id)": "@(1,id).@(1,typeName).id",
                    "@(1,action)": "@(1,id).@(1,typeName).action",
                    "*": "@(1,id).@(1,typeName).&1.&" // &1 replicates key name "type"
                  }
                }
              },
              "type": {
                "@(1,id)": "@(1,id).@(1,typeName).id",
                "@(1,action)": "@(1,id).@(1,typeName).action",
                "*": "@(1,id).@(1,typeName).&1.&" // & replicates the values of the elements nested within the "type" object
              }
            }
          },
          "type": {
            "@(1,id)": "@(1,id).@(1,typeName).id",
            "@(1,action)": "@(1,id).@(1,typeName).action",
            "*": "@(1,id).@(1,typeName).&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "xx": ""
      }
    }
  }
]

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

在此处输入图像描述

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

https://github.com/octomix/josson

反序列化

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"id\": 11," +
    "  \"item\": [" +
    "    {" +
    "      \"id\": \"11_1\"," +
    "      \"action\": \"add\"," +
    "      \"type\": {" +
    "        \"id\": \"11_1_xx\"," +
    "        \"typeName\": \"xx\"" +
    "      }," +
    "      \"item\": [" +
    "        {" +
    "          \"id\": \"11_1_1\"," +
    "          \"action\": \"add\"," +
    "          \"type\": {" +
    "            \"id\": \"11_1_1_zz\"," +
    "            \"typeName\": \"zz\"" +
    "          }," +
    "          \"item\": [" +
    "            {" +
    "              \"id\": \"11_1_1_1\"," +
    "              \"action\": \"add\"," +
    "              \"type\": {" +
    "                \"id\": \"11_1_1_1_xx\"," +
    "                \"typeName\": \"xx\"" +
    "              }" +
    "            }" +
    "          ]" +
    "        }," +
    "        {" +
    "          \"id\": \"11_1_2\"," +
    "          \"action\": \"add\"," +
    "          \"type\": {" +
    "            \"id\": \"11_1_2_xx\"," +
    "            \"typeName\": \"xx\"" +
    "          }," +
    "          \"item\": [" +
    "            {" +
    "              \"id\": \"11_1_2_1\"," +
    "              \"action\": \"add\"," +
    "              \"type\": {" +
    "                \"id\": \"11_1_2_1_zz\"," +
    "                \"typeName\": \"zz\"" +
    "              }" +
    "            }" +
    "          ]" +
    "        }" +
    "      ]" +
    "    }" +
    "  ]" +
    "}");
    

转型

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

Output

[ {
  "id" : "11_1",
  "action" : "add",
  "type" : {
    "id" : "11_1_xx",
    "typeName" : "xx"
  }
}, {
  "id" : "11_1_2",
  "action" : "add",
  "type" : {
    "id" : "11_1_2_xx",
    "typeName" : "xx"
  }
}, {
  "id" : "11_1_1_1",
  "action" : "add",
  "type" : {
    "id" : "11_1_1_1_xx",
    "typeName" : "xx"
  }
} ]

暂无
暂无

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

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