arrays - Jolt:如何合并来自不同数组的一个数组元素
问题描述
我正在尝试合并数组中不同元素的内容。
我的json看起来像这样:
{
"Messages": {
"test": "test1",
"CreditCheck": [
{
"name": "Credit Score",
"score": 15,
"percentage": 20
},
{
"name": "Cards Score",
"score": 15,
"percentage": 20
},
{
"name": "Bank Score",
"score": 20,
"percentage": 20
}
]
}
}
我目前的 Jolt Spec 是这样的:
[
{
"operation": "shift",
"spec": {
"*": {
"test": "myTesting",
"CreditCheck": {
"*": {
"name": "creditName.[]",
"score": "creditScore.[]"
}
}
}
}
}
]
我得到以下信息:
{
"myTesting" : "test1",
"creditName" : [ "Credit Score", "Cards Score", "Bank Score" ],
"creditScore" : [ 15, 15, 20 ]
}
但我需要实现的是以下几点:
{
"myTesting" : "test1",
"creditNames&Scores" : [ "Credit Score": 15, "Cards Score": 15, "Bank Score": 20 ],
}
“CreditCheck”结构中没有预定义数量的元素,所以我试图让它尽可能动态。
提前感谢您的帮助,最好的!
解决方案
该规范应该适合您:
[
{
"operation": "shift",
"spec": {
"*": {
"test": "myTesting",
"CreditCheck": {
"*": {
"score": "creditNames\\&Scores[].@(1,name)"
}
}
}
}
}
]
输出:
{
"myTesting" : "test1",
"creditNames&Scores" : [ { "Credit Score" : 15 }, { "Cards Score" : 15 }, { "Bank Score" : 20} ]
}
笔记:
- 输出中的元素必须用大括号括起来才能使它们成为有效的 json 对象
\\
用于转义 & 符号&
- 使用https://jolt-demo.appspot.com/进行测试,您还可以在其中找到高级通配符用法的示例
@
推荐阅读
- c# - 如何创建链接按钮以从网格视图中删除一行
- flutter - 如何在 CustomScrollView 中的另一个小部件上检测定位小部件上的点击(或任何其他手势事件)?
- c# - 从子类(或子对象)C# 和 ASP.NET MVC 搭建视图
- flutter - 如何将图标按钮动态更改为字符串或表情符号
- java - 使用spring boot hashmap创建动态对象
- visual-studio - 用于本地调试 VS 2019 的 Azure 函数流式处理日志
- arrays - 从符号矩阵到数值的大量替换,同时将 4 个未知数保留到新矩阵中
- docker - 从 Windows 主机共享目录到 Kubernetes 容器(使用 Docker 桌面)
- php - 使用 mysqli_stmt_bind_result 时在哪一行检查数据获取是否成功?
- .net - Powershell cmdlet 与 .NET