结合不同属性的Jolt规范

问题描述 投票:0回答:1

我正在尝试将键移动到另一个 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"
      }
    }
  }
}

是否可以修改我当前的震动规格以涵盖上述两种情况?

json transformation jolt
1个回答
0
投票

您可以在第一个规范中分别选择带有键

"Ids"
"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": "&",
      "*": {
        "*": "&[]"
      }
    }
  }
]

这是两个输入的常见转换。

© www.soinside.com 2019 - 2024. All rights reserved.