简体   繁体   English

JOLT 移位转换:按属性的内部值(不是名称)过滤 - 仅某些属性

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

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

My goal is to get an array that contains only the items with the typeName 'xx'.我的目标是获得一个仅包含类型名称为“xx”的项目的数组。 But not all the item object, only some of the fields但不是所有的item object,只有部分字段

{
  "id": "11_1",
  "action": "add",
  "payment": {
    "paied": true,
    "coin": "dollar"
  },
  "type": {
    "id": "11_1_xx",
    "typeName": "xx"
  },
  "details": {
    "place": {
      "id": "123",
      "name": "xx"
    },
    "status": {
      "id": "123",
      "name": "xx"
    }
  }
}

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

Input :输入

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "payment": {
        "paied": true,
        "coin": "dollar"
      },
      "type": {
        "id": "11_1_xx",
        "typeName": "xx",
        "typeGroup": "xx",
        "typeValue": "xx"
      },
      "details": {
        "place": {
          "id": "123",
          "name": "xx"
        },
        "status": {
          "id": "123",
          "name": "xx"
        },
        "reason": {
          "id": "123",
          "name": "xx"
        }
      },
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "payment": {
            "paied": true,
            "coin": "dollar"
          },
          "type": {
            "id": "11_1_1_zz",
            "typeName": "zz",
            "typeGroup": "zz",
            "typeValue": "zz"
          },
          "details": {
            "place": {
              "id": "123",
              "name": "xx"
            },
            "status": {
              "id": "123",
              "name": "xx"
            },
            "reason": {
              "id": "123",
              "name": "xx"
            }
          },
          "item": [
            {
              "id": "11_1_1_1",
              "action": "add",
              "payment": {
                "paied": true,
                "coin": "NIS"
              },
              "type": {
                "id": "11_1_1_1_xx",
                "typeName": "xx",
                "typeGroup": "xx",
                "typeValue": "xx"
              },
              "details": {
                "place": {
                  "id": "123",
                  "name": "xx"
                },
                "status": {
                  "id": "123",
                  "name": "xx"
                },
                "reason": {
                  "id": "123",
                  "name": "xx"
                }
              }
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "add",
          "payment": {
            "paied": false,
            "coin": "dollar"
          },
          "type": {
            "id": "11_1_2_xx",
            "typeName": "xx",
            "typeGroup": "xx",
            "typeValue": "xx"
          },
          "details": {
            "place": {
              "id": "123",
              "name": "xx"
            },
            "status": {
              "id": "123",
              "name": "xx"
            },
            "reason": {
              "id": "123",
              "name": "xx"
            }
          },
          "item": [
            {
              "id": "11_1_2_1",
              "action": "add",
              "payment": {
                "paied": false,
                "coin": "NIS"
              },
              "type": {
                "id": "11_1_2_1_zz",
                "typeName": "zz",
                "typeGroup": "zz",
                "typeValue": "zz"
              },
              "details": {
                "place": {
                  "id": "123",
                  "name": "xx"
                },
                "status": {
                  "id": "123",
                  "name": "xx"
                },
                "reason": {
                  "id": "123",
                  "name": "xx"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

Expected output :预计 output

[
  {
    "id": "11_1",
    "action": "add",
    "payment": {
      "paied": true,
      "coin": "dollar"
    },
    "type": {
      "id": "11_1_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  },
  {
    "id": "11_1_1_1",
    "action": "add",
    "payment": {
      "paied": true,
      "coin": "NIS"
    },
    "type": {
      "id": "11_1_1_1_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  },
  {
    "id": "11_1_2",
    "action": "add",
    "payment": {
      "paied": false,
      "coin": "dollar"
    },
    "type": {
      "id": "11_1_2_xx",
      "typeName": "xx"
    },
    "details": {
      "place": {
        "id": "123",
        "name": "xx"
      },
      "status": {
        "id": "123",
        "name": "xx"
      }
    }
  }
]

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

You can use the following explained spec您可以使用以下解释的规范

[
  {
    // Determine all key-value pairs partitioned under the main value and type.typeValue combinations 
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "type": {
                    "@(1,id)": "@(2,id).@(1,typeName).id", //traverse } character twice in order to reach the main id of the object
                    "@(1,action)": "@(2,id).@(1,typeName).action",
                    "*": "@(2,id).@(1,typeName).&1.&" // &1 replicates "type" key, & does the leaf value
                  },
                  "*": "@(1,id).@(1,type.typeName).&"
                }
              },
              "type": {
                "@(1,id)": "@(2,id).@(1,typeName).id",
                "@(1,action)": "@(2,id).@(1,typeName).action",
                "*": "@(2,id).@(1,typeName).&1.&"
              },
              "*": "@(1,id).@(1,type.typeName).&"
            }
          },
          "type": {
            "@(1,id)": "@(2,id).@(1,typeName).id",
            "@(1,action)": "@(2,id).@(1,typeName).action",
            "*": "@(2,id).@(1,typeName).&1.&"
          },
          "*": "@(1,id).@(1,type.typeName).&"
        }
      }
    }
  },
  {
    // Filter out by the desired value(in this case it's "xx") 
    "operation": "shift",
    "spec": {
      "*": {
        "xx": ""
      }
    }
  },
  {
    // Get rid of same repeating components of id and action arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "id": "ONE",
        "action": "ONE"
      }
    }
  },
  {
    // Get rid of undesired attributes
    "operation": "remove",
    "spec": {
      "*": {
        "type": {
          "typeGroup": "",
          "typeValue": ""
        },
        "det*": {
          "reason": ""
        }
      }
    }
  }
]

You may try library Josson .您可以尝试图书馆Josson Function map() build a new ObjectNode. Function map()构建一个新的 ObjectNode。 Function field() modify the current ObjectNode and can be used to remove fields. Function field()修改当前ObjectNode,可用于移除字段。 The transformation statement is short and easy to understand.转换语句简短易懂。

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

Josson josson = Josson.fromJsonString(inputJSON);
JsonNode node = josson.getNode(
    "cumulateCollect(" +
    "    [type.typeName='xx']" +              // filter
    "        .field(type.map(id, typeName)" + // need "type.id" and "type.typeName"
    "              ,details.field(reason:)" + // remove "details.reason"
    "              ,item:)" +                 // remove "item"
    "   ,item)");                             // next round
System.out.println(node.toPrettyString());

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

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