简体   繁体   English

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

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

I am trying to transform a JSON using Jolt transformation looking for some input here.我正在尝试使用 Jolt 转换来转换 JSON,在此处寻找一些输入。 I am trying to filter by the value of the attribute.我正在尝试按属性的值进行过滤。

My goal is to get an array that contains only the items with the action 'remove'.我的目标是获得一个仅包含具有“删除”操作的项目的数组。

Here is my input and expected output:这是我的输入和预期的 output:

Input :输入

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}

Expected output :预期 output

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

Can you please help me to write a simple spec that will do this?你能帮我写一个简单的规范来做到这一点吗?

  • Let's flatten the JSON by determining the cases to be "item" vs. else case("*") while walking the tree within the first spec让我们通过在第一个规范中遍历树时确定案例是"item"else case("*")来展平 JSON

  • and label the individual arrays by their action names和 label 个人 arrays 由他们的action名称

  • and then pick objects of the remove array the last spec然后选择remove数组的对象最后一个规范

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

the demo on the playground site for jolt( http://jolt-demo.appspot.com/ ) is jolt( http://jolt-demo.appspot.com/ )在操场上的演示是

在此处输入图像描述

You may consider another library Josson that a simple statement can do the job.您可以考虑另一个库Josson ,一个简单的语句就可以完成这项工作。 And the function even supports unknown and unlimited number of path levels. function 甚至支持未知和无限数量的路径级别。

https://github.com/octomix/josson https://github.com/octomix/josson

Deserialization反序列化

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\"" +
    "            }" +
    "          ]" +
    "        }" +
    "      ]" +
    "    }" +
    "  ]" +
    "}");
    

Transformation转型

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

Output 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