如何使用Jolt将一个数组映射,输入数组的另外两个元素中包含键和值。

huangapple go评论60阅读模式
英文:

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/上的演示如下:

如何使用Jolt将一个数组映射,输入数组的另外两个元素中包含键和值。

英文:

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 :

如何使用Jolt将一个数组映射,输入数组的另外两个元素中包含键和值。

huangapple
  • 本文由 发表于 2023年7月20日 16:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定