英文:
how to map an array by jolt, there are keys and values in other 2 elements of input array
问题
{
"input": {
"data": [
[
"key1",
"key2.subkey2.1",
"key3.0.subkey3.0",
"key3.1.subkey3.1"
],
[
"valuekey1",
"valueSubkey2.0",
"valueSubkey3.0",
"valueSubkey3.1"
]
]
},
"desired output": {
"key1": "valuekey1",
"key2": {
"subkey2.1": "valueSubkey2.0"
},
"key3": [
{
"subkey3.0": "valueSubkey3.0"
},
{
"subkey3.1": "valueSubkey3.1"
}
]
}
}
英文:
First time JOLT user trying to restructure an array into a similar multi-level array. stackoverflow is asking for less code and more words, so I hope this extra sentence covers that.
input:
{
"data": [
[ // KEYS
"key1",
"key2.subkey2.1",
"key3.0.subkey3.0",
"key3.1.subkey3.1"
],
[ // VALUES OF above keys
"valuekey1",
"valueSubkey2.0",
"valueSubkey3.0",
"valueSubkey3.1"
]
]
}
desired output:
{
"key1": "valuekey1",
"key2": {
"subkey2.1": "valueSubkey2.0"
},
"key3": [
{
"subkey3.0": "valueSubkey3.0"
},
{
"subkey3.1": "valueSubkey3.1"
}
]
}
Thank you all for your help
答案1
得分: 1
你可以使用以下的转换:
[
{
"operation": "shift",
"spec": {
"data": {
"0": {
"*": {
"@(2,[1][&])": "@(3,[0][&1])"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"*.sub*": {
"@": "&(1,1).sub&(1,2)"
},
"*.*.sub*": {
"@": "&(1,1)[&(1,2)].sub&(1,3)"
}
}
}
]
其中
&(1,2),作为示例,表示在第1个上层的第2个星号的文字替换
-
在第1个规则中:匹配所有相应的对应项,以便将索引为0的值与索引为1的值组成四个单独的键值对。
-
在第2个规则中:通过点来分隔键,不忽略
sub
文字。
网站http://jolt-demo.appspot.com/上的演示如下:
英文:
You can use the following transformation :
[
{
"operation": "shift",
"spec": {
"data": {
"0": { // the array already has only one outermost component
"*": {
"@(2,[1][&])": "@(3,[0][&1])"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"*.sub*": {
"@": "&(1,1).sub&(1,2)"
},
"*.*.sub*": {
"@": "&(1,1)[&(1,2)].sub&(1,3)"
}
}
}
]
where
&(1,2), as an example, represents the literal replacement for the 2nd asterisk from the 1 upper level
-
in the 1st spec : Match all the respective counterparts so as to
stand the value with index 0 vs. with index 1 to form four
individual key-value pairs -
in the 2nd spec : Partition the keys by the dots without ignoring the
sub
literals
the demo on the site http://jolt-demo.appspot.com/ is :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论