json 震动规格以合并不同属性

clj7thdc  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(89)

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


我期望的输出是这样的:Case 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"
      }
    }
  }
}


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

pwuypxnk

pwuypxnk1#

您可以在第一个规范中分别选择键为"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": "&",
      "*": {
        "*": "&[]" // keep square brackets considering the cases similar to the second one
      }
    }
  }
]

字符串
这是两个输入的公共变换。

相关问题