Jolt 规范可组合不同的属性

Jolt spec to combine different attributes

提问人:Aditya Ashok 提问时间:11/17/2023 最后编辑:Barbaros ÖzhanAditya Ashok 更新时间:11/17/2023 访问量:27

问:

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

案例一:输入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"
      }
    }
  }
}

案例二:输入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 转换 震撼

评论


答:

1赞 Barbaros Özhan 11/17/2023 #1

您可以分别选择带有键 vs. 的对象。 在第一个规范中,同时提取要在第二个规范中使用的文字,该规范将在左侧包含 # 运算符以硬编码所需的文字,例如"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
      }
    }
  }
]

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

评论

0赞 Aditya Ashok 11/17/2023
当然,完成了。我有一个关于同一主题的后续问题。有什么办法可以联系到你吗?