使用 Jolt 变换从多个 JSON 数组中提取属性
Extract properties from multiple JSON arrays using Jolt transformation
我的 JSON 对象如下所示:
{
"array1": [
{
"key1": "value1", // common key
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1", // common key
"key2": "value2",
"key3": "value3"
}
],
"includes": {
"array2": [
{
"key1": "value1", // common key
"key4": "value4",
"key5": "value5"
},
{
"key1": "value1",
"key4": "value4",
"key5": "value5"
}
]
}
}
我需要以下格式的输出 -
[
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4", // this comes from joining with array 2 based on key1
"key5": "value5" // this comes from joining with array 2 based on key1
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4", // this comes from joining with array 2 based on key1
"key5": "value5" // this comes from joining with array 2 based on key1
}
]
我只有从 array1 获取字段的解决方案,但不确定如何根据公共键与 array2 连接,获取所需字段并以所需方式表示它们。
当前转换:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"key1": "[&1].key1",
"key2": "[&1].key2",
"key3": "[&1].key3"
}
}
}
}
]
当前不需要的输出:
[
{
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
{
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
]
如有任何帮助,我们将不胜感激。谢谢!
首先,为了获得“不需要的输出”,需要将"data"
替换为"*"
当前转换规范内的通配符,无需重复每个属性键名称和值分支,仅使用此规范即可
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "[&1].&"
}
}
}
}
]
如果你再嵌套一层
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": "[&1].&"
}
}
}
}
}
]
那么,你会得到
[
{
"key1" : "value1",
"key4" : "value4",
"key5" : "value5"
},
{
"key1" : "value1",
"key4" : "value4",
"key5" : "value5"
}
]
我们可以在不同级别的对象上使用"*"
和"@"
通配符来组合这些对象结果,但在这种情况下,键名 "key1"
的值当然会重复。我们可以通过添加 cardinality transformation 来摆脱重复,并得到您想要的结果,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": "[&1].&"
},
"@": "[&1]"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
}
]
站点 http://jolt-demo.appspot.com/ 上的 演示 是:
我的 JSON 对象如下所示:
{
"array1": [
{
"key1": "value1", // common key
"key2": "value2",
"key3": "value3"
},
{
"key1": "value1", // common key
"key2": "value2",
"key3": "value3"
}
],
"includes": {
"array2": [
{
"key1": "value1", // common key
"key4": "value4",
"key5": "value5"
},
{
"key1": "value1",
"key4": "value4",
"key5": "value5"
}
]
}
}
我需要以下格式的输出 -
[
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4", // this comes from joining with array 2 based on key1
"key5": "value5" // this comes from joining with array 2 based on key1
},
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4", // this comes from joining with array 2 based on key1
"key5": "value5" // this comes from joining with array 2 based on key1
}
]
我只有从 array1 获取字段的解决方案,但不确定如何根据公共键与 array2 连接,获取所需字段并以所需方式表示它们。
当前转换:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"key1": "[&1].key1",
"key2": "[&1].key2",
"key3": "[&1].key3"
}
}
}
}
]
当前不需要的输出:
[
{
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
},
{
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
]
如有任何帮助,我们将不胜感激。谢谢!
首先,为了获得“不需要的输出”,需要将"data"
替换为"*"
当前转换规范内的通配符,无需重复每个属性键名称和值分支,仅使用此规范即可
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "[&1].&"
}
}
}
}
]
如果你再嵌套一层
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": "[&1].&"
}
}
}
}
}
]
那么,你会得到
[
{
"key1" : "value1",
"key4" : "value4",
"key5" : "value5"
},
{
"key1" : "value1",
"key4" : "value4",
"key5" : "value5"
}
]
我们可以在不同级别的对象上使用"*"
和"@"
通配符来组合这些对象结果,但在这种情况下,键名 "key1"
的值当然会重复。我们可以通过添加 cardinality transformation 来摆脱重复,并得到您想要的结果,例如
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": "[&1].&"
},
"@": "[&1]"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
}
]
站点 http://jolt-demo.appspot.com/ 上的 演示 是: