我正在try 将密钥移动到另一个JSON密钥的值.目标值是一个数组,我正在try 向该数组添加一个元素.

case 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"
      }
    }
  }
}

case 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"
  } ]
}

case 2预期yields :

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

我目前的震荡规格:

[
  {
    "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"
      }
    }
  }
}

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

推荐答案

您可以在第一个规范中使用键"Ids""OtherIds"分别 Select 对象,同时提取要在第二个规范中使用的文字"Ids",第二个规范将在左侧包含103操作符,以硬编码所需的文字,例如

[
  {
    "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
      }
    }
  }
]

这是两个输入的共同转换.

Json相关问答推荐

无法根据vega规范中的条件设置文本 colored颜色

Bash和echo命令出现意外结果

使用动态和可变JSON的图表多条

Golang返回的JSON顶级字段是可变的.如何在 struct 中使用

NIFI-我需要数组的信息,但只需要第一个信息

递归解码嵌套列表(具有任意深度的列表列表)

如何将该 JSON 解析为 Kotlin 类?

将 colly 包输出文本添加到 golang 中的映射

如何在 Dart 中与多个 map (字典)相交

Powershell JSON 操作

在 C# 中解析 Json rest api 响应

在 JavaScript 中从 Json 数据中删除反斜杠

IE中Json响应下载(7~10)

使用 JSONObject 在 Java 中为以下 struct 创建嵌套 JSON 对象?

JSON对象中的JavaScript递归搜索

如何在不消除对象歧义的情况下使用 circe 解码 ADT

Spring Security 和 JSON 身份验证

Gson 将一组数据对象转换为 json - Android

有 Json 标签但未导出

在没有 ASP.NET Core 的情况下将 IConfigurationSection 绑定到复杂对象