繁体   English   中英

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

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

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

我的目标是获得一个仅包含类型名称为“xx”的项目的数组。 但不是所有的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"
    }
  }
}

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

输入

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

预计 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"
      }
    }
  }
]

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

您可以使用以下解释的规范

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

您可以尝试图书馆Josson Function map()构建一个新的 ObjectNode。 Function field()修改当前ObjectNode,可用于移除字段。 转换语句简短易懂。

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