簡體   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