代码之家  ›  专栏  ›  技术社区  ›  Aditya Ashok

Jolt规范可组合不同的属性

  •  1
  • Aditya Ashok  · 技术社区  · 2 年前

    我正在尝试将该键移动到另一个JSON键的值。目标值是一个数组,我正试图向该数组添加一个元素。

    案例1:输入json对象:

    {
      "Name": {
        "PRI": {
          "firstName": "Joe"
        }
      },
      "Ids": {
        "IND": {
          "IND-ADR": {
            "id": "ind-adr-id",
            "key": "ind1"
          },
          "IND-PAN": {
            "id": "ind-pan-id",
            "key": "ind2"
          }
        },
        "USA": {
          "USA-SSN": {
            "id": "usa-ssn-id",
            "key": "usa1"
          }
        }
      },
      "OtherIds": {
        "1970-01-01": {
          "0": {
            "idLast": "2023"
          }
        }
      }
    }
    

    案例2:输入json对象:

    {
      "Name": {
        "PRI": {
          "firstName": "Joe"
        }
      },
      "OtherIds": {
        "1970-01-01": {
          "0": {
            "idLast": "2023"
          }
        }
      }
    }
    

    我期望的输出是这样的: 案例1预期输出:

    {
      "Name" : {
        "PRI" : {
          "firstName" : "Joe"
        }
      },
      "Ids" : [ {
        "country" : "IND",
        "IdType" : "IND-ADR",
        "id" : "ind-adr-id",
        "key" : "ind1"
      }, {
        "country" : "IND",
        "IdType" : "IND-PAN",
        "id" : "ind-pan-id",
        "key" : "ind2"
      }, {
        "country" : "USA",
        "IdType" : "USA-SSN",
        "id" : "usa-ssn-id",
        "key" : "usa1"
      }, { //from OtherIds
        "country" : "USA", // hard-coding this
        "IdType" : "SSN Tail", //hard-coding this
        "idLast" : "2023"
      } ]
    }
    

    情况2预期输出:

    {
      "Name" : {
        "PRI" : {
          "firstName" : "Joe"
        }
      },
      "Ids" : [ {//from OtherIds
        "country" : "USA", // hard-coding this
        "IdType" : "SSN Tail", //hard-coding this
        "idLast" : "2023"
      } ]
    }
    

    我当前的JOLT规范:

    [
      {
        "operation": "shift",
        "spec": {
          "Ids": {
            "*": {
              "*": {
                "$1": "NID.&2_&1.&3.country",
                "$": "NID.&2_&1.&3.IdType",
                "*": "NID.&2_&1.&3.&"
              }
            }
          },
          "*": "&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "NID": {
            "*": {
              "*": {
                "*": "&1[#3].&"
              }
            }
          },
          "*": "&"
        }
      }
    ]
    

    我的当前输出:

    {
      "Name" : {
        "PRI" : {
          "firstName" : "Joe"
        }
      },
      "Ids" : [ {
        "country" : "IND",
        "IdType" : "IND-ADR",
        "id" : "ind-adr-id",
        "key" : "ind1"
      }, {
        "country" : "IND",
        "IdType" : "IND-PAN",
        "id" : "ind-pan-id",
        "key" : "ind2"
      }, {
        "country" : "USA",
        "IdType" : "USA-SSN",
        "id" : "usa-ssn-id",
        "key" : "usa1"
      } ],
      "OtherIds" : {
        "1970-01-01" : {
          "0" : {
            "idLast" : "2023"
          }
        }
      }
    }
    

    有没有可能修改我目前的震动规格,以涵盖上述两种情况?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Barbaros Özhan    2 年前

    您可以使用键分别拾取对象 "Ids" vs。 "OtherIds" 在第一个规范中,同时提取文字 “Ids” 将在包含 # 左侧的运算符来硬编码所需的文字,例如

    [
      {
        "operation": "shift",
        "spec": {
          "*": { // else case, eg. Ids
            "*": {
              "*": {
                "$1": "&2_&1.&3.country",
                "$": "&2_&1.&3.IdType",
                "*": "&2_&1.&3.&"
              }
            }
          },
          "Other*": {
            "*": {
              "*": {
                "#USA": "&2_&1.&(3,1).country", // bring the value of the asterisk within "Other*", eg. Ids
                "#SSN Tail": "&2_&1.&(3,1).IdType",
                "*": "&2_&1.&(3,1).&"
              }
            }
          },
          "Name": "&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Name": "&",
          "*": {
            "*": "&[]" // keep square brackets considering the cases similar to the second one
          }
        }
      }
    ]
    

    这是两个输入的共同变换。

    推荐文章