使用Jolt更改目标字段

时间:2017-03-06 15:46:28

标签: json jolt

我有几种情况需要将地图字段转换为有时嵌套几层深度的数组字段。这些是文档中唯一需要更改的字段,因此其他字段不需要对它们执行任何类型的转换。我目前的方法是复制每个级别的未更改字段,如下所示:

[
  {
    "operation": "shift",
    "spec": {
      "agentsMetrics": {
        "metricsPerAgent": {
          "*": {
            "$": "agentsMetrics.metricsPerAgent[#2].agentId",
            "@": "agentsMetrics.metricsPerAgent[#2].value"
          }
        },
        "*": {
          "@": "agentsMetrics.&"
        }
      },
      "skillsMetricsPerAgent": {
        "metricsPerSkill": {
          "*": {
            "$": "skillsMetricsPerAgent.metricsPerSkill[#2].skillId",
            "metricsPerAgent": {
              "*": {
                "$": "skillsMetricsPerAgent.metricsPerSkill[#4].metricsPerAgent[#2].agentId",
                "@": "skillsMetricsPerAgent.metricsPerSkill[#4].metricsPerAgent[#2].value"
              }
            },
            "*": {
              "@": "skillsMetricsPerAgent.metricsPerSkill[#3].&"
            }
          }
        },
        "*": {
          "@": "skillsMetricsPerAgent.&"
        }
      }
    }
  }
]

我的输入如下:

{
    "agentsMetrics": {
        "metricsTotals": {
            "connectedEngagements": 70,
            "nonInteractiveTotalHandlingTime": 309,
            "totalHandlingTime": 47696,
            "totalNonInteractiveChats": 2,
            "totalInteractiveChats": 73
        },
        "metricsPerAgent": {
            "645355412": {
                "connectedEngagements": 2,
                "nonInteractiveTotalHandlingTime": 0,
                "totalHandlingTime": 1718,
                "totalNonInteractiveChats": 0,
                "totalInteractiveChats": 2
            },
            "645366912": {
                "connectedEngagements": 1,
                "nonInteractiveTotalHandlingTime": 0,
                "totalHandlingTime": 488,
                "totalNonInteractiveChats": 0,
                "totalInteractiveChats": 1
            }
        }
    },
    "skillsMetricsPerAgent": {
        "metricsTotals": {
            "connectedEngagements": 70,
            "nonInteractiveTotalHandlingTime": 309,
            "totalHandlingTime": 47696,
            "totalNonInteractiveChats": 2,
            "totalInteractiveChats": 73
        },
        "metricsPerSkill": {
            "641431612": {
                "metricsTotals": {
                    "connectedEngagements": 7,
                    "nonInteractiveTotalHandlingTime": 0,
                    "totalHandlingTime": 6377,
                    "totalNonInteractiveChats": 0,
                    "totalInteractiveChats": 8
                },
                "metricsPerAgent": {
                    "645355312": {
                        "connectedEngagements": 1,
                        "nonInteractiveTotalHandlingTime": 0,
                        "totalHandlingTime": 115,
                        "totalNonInteractiveChats": 0,
                        "totalInteractiveChats": 1
                    },
                    "645365512": {
                        "connectedEngagements": 0,
                        "nonInteractiveTotalHandlingTime": 0,
                        "totalHandlingTime": 766,
                        "totalNonInteractiveChats": 0,
                        "totalInteractiveChats": 1
                    }
                }
            },
            "1218517512": {
                "metricsTotals": {
                    "connectedEngagements": 2,
                    "nonInteractiveTotalHandlingTime": 0,
                    "totalHandlingTime": 1379,
                    "totalNonInteractiveChats": 0,
                    "totalInteractiveChats": 2
                },
                "metricsPerAgent": {
                    "645367512": {
                        "connectedEngagements": 1,
                        "nonInteractiveTotalHandlingTime": 0,
                        "totalHandlingTime": 571,
                        "totalNonInteractiveChats": 0,
                        "totalInteractiveChats": 1
                    },
                    "645378812": {
                        "connectedEngagements": 1,
                        "nonInteractiveTotalHandlingTime": 0,
                        "totalHandlingTime": 808,
                        "totalNonInteractiveChats": 0,
                        "totalInteractiveChats": 1
                    }
                }
            }
        }
    }
}

有没有办法定位特定的字段并自己操作它们,同时保留其他所有字段?在这种情况下,我想定位metricsPerAgent和metricsPerSkill。

2 个答案:

答案 0 :(得分:1)

有没有办法定位特定的字段并自己操作它们,同时保留其他所有字段?

否/没有班次/目前没有。注意" shift"操作正在从输入到输出进行复制。

答案 1 :(得分:0)

这种方法似乎有效(不过,尚未进行全面测试或评估效率):

{
    "operation": "shift",
    "spec": {
        // derived from https://stackoverflow.com/questions/40494231/re-parent-a-json-object-using-jolt#40513842
        "*": {
            "@": "&"
        },
        // any specific shifts go here
    }
}