“`如何使用Jolt将数组插入特定数组元素?“`

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

How can I insert an array into a specific array element using Jolt

问题

以下是您要翻译的内容:

Question/problem

如何使用Jolt将数组插入到数组中的特定对象?

input

这些对象具有一组共同的属性,但支持可选属性。

特定对象需要根据它们在数组中的特定位置,即第二个([1]),获取这些可选属性。这些可选属性将作为jolt转换的一部分添加。

{
  "array" : [
   {
     "key" : "a key",
     "value" : "a value"
   },
   {
     "key" : "another key",
     "value" : "another value"
   }
  ]
}

expected

{
  "array" : [
   {
     "key" : "a key",
     "value" : "a value"
   },
   {
     "key" : "another key",
     "value" : "another value",
     "values":["extra value1", "extra value2"]
   }
  ]
}

What has been tried

我尝试过defaultmodify-default-betamodify-overwrite-beta,但似乎都不按我想象的方式工作。

default

似乎默认情况下会忽略数组引用"[1]",所以我尝试了似乎支持此操作的beta操作。

default spec

[
  {
    "operation": "default",
    "spec": {
      "array": {
        "[1]": {
          "values": ["extra value1", "extra value2"]
        }
      }
    }
  }
]

default actual

{
  "array" : [ {
    "key" : "a key",
    "value" : "a value"
  }, {
    "key" : "another key",
    "value" : "another value"
  } ]
}

modify-default-beta

解析了数组引用"[1]",但仅将规范中的数组的第一个元素应用于数组的整个部分。

modify-default-beta spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "array": {
        "[1]": {
          "values": ["extra value1", "extra value2"]
        }
      }
    }
  }
]

modify-default-beta actual

{
  "array" : [ {
    "key" : "a key",
    "value" : "a value"
  }, {
    "key" : "another key",
    "value" : "another value",
    "values" : "extra value1" // 数组被丢弃
  } ]
}

modify-overwrite-beta

尝试modify-overwrite-beta,结果与modify-default-beta相同。

modify-overwrite-beta spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "array": {
        "[1]": {
          "values": ["extra value1", "extra value2"]
        }
      }
    }
  }
]

modify-overwrite-beta actual

{
  "array" : [ {
    "key" : "a key",
    "value" : "a value"
  }, {
    "key" : "another key",
    "value" : "another value",
    "values" : "extra value1" // 数组被丢弃
  } ]
}

Interesting side note

没有输入中的数组引用,这是一个相当简单的问题,似乎当存在数组引用和*-beta操作时,行为不是我期望的。

simple default

我从输入中去掉了数组

simple default input

{
  "element1": {
    "key": "a key",
    "value": "a value"
  },
  "element2": {
    "key": "a key",
    "value": "another value"
  }
}

simple default spec

[
  {
    "operation": "default",
    "spec": {
      "element2": {
        "values": ["extra value1", "extra value2"]
      }
    }
  }
]

simple default actual

{
  "element1" : {
    "key" : "a key",
    "value" : "a value"
  },
  "element2" : {
    "key" : "another key",
    "value" : "another value",
    "values" : [ "extra value1", "extra value2" ]
  }
}

<details>
<summary>英文:</summary>

# Question/problem

How can I use [Jolt][1] to insert an array into a specific object in an array? 

# input

The objects have a common set of attributes, but support optional attributes.

Specific objects need to get these optional attributes depending on their specific position in the array, i.e. second (`[1]`). These optional attributes are to be added as part of the jolt transformation.

```json
{
  &quot;array&quot; : [
   {
     &quot;key&quot; : &quot;a key&quot;,
     &quot;value&quot; : &quot;a value&quot;
   },
   {
     &quot;key&quot; : &quot;another key&quot;,
     &quot;value&quot; : &quot;another value&quot;
   }
  ]
}

expected

{
  &quot;array&quot; : [
   {
     &quot;key&quot; : &quot;a key&quot;,
     &quot;value&quot; : &quot;a value&quot;
   },
   {
     &quot;key&quot; : &quot;another key&quot;,
     &quot;value&quot; : &quot;another value&quot;,
     &quot;values&quot;: [&quot;extra value1&quot;, &quot;extra value2&quot;]
   }
  ]
}

What has been tried

I've tried default, modify-default-beta, and modify-overwrite-beta and none seem to behave the way I thought they did.

default

Seems like default ignores the array reference &quot;[1]&quot; so tried the beta operations which seem to support this.

default spec

[
  {
    &quot;operation&quot;: &quot;default&quot;,
    &quot;spec&quot;: {
      &quot;array&quot;: {
        &quot;[1]&quot;: {
          &quot;values&quot;: [&quot;extra value1&quot;, &quot;extra value2&quot;]
        }
      }
    }
  }
]

default actual

{
  &quot;array&quot; : [ {
    &quot;key&quot; : &quot;a key&quot;,
    &quot;value&quot; : &quot;a value&quot;
  }, {
    &quot;key&quot; : &quot;another key&quot;,
    &quot;value&quot; : &quot;another value&quot;
  } ]
}

modify-default-beta

Resolves the array reference &quot;[1]&quot;, but only applies the first element in the array from the spec and not the whole array

modify-default-beta spec

[
  {
    &quot;operation&quot;: &quot;modify-default-beta&quot;,
    &quot;spec&quot;: {
      &quot;array&quot;: {
        &quot;[1]&quot;: {
          &quot;values&quot;: [&quot;extra value1&quot;, &quot;extra value2&quot;]
        }
      }
    }
  }
]

modify-default-beta actual

{
  &quot;array&quot; : [ {
    &quot;key&quot; : &quot;a key&quot;,
    &quot;value&quot; : &quot;a value&quot;
  }, {
    &quot;key&quot; : &quot;another key&quot;,
    &quot;value&quot; : &quot;another value&quot;,
    &quot;values&quot; : &quot;extra value1&quot; // array is dumped
  } ]
}

modify-overwrite-beta

Tried modify-overwrite-beta and I get the same behaviour as modify-default-beta.

modify-overwrite-beta spec

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;array&quot;: {
        &quot;[1]&quot;: {
          &quot;values&quot;: [&quot;extra value1&quot;, &quot;extra value2&quot;]
        }
      }
    }
  }
]

modify-overwrite-beta actual

{
  &quot;array&quot; : [ {
    &quot;key&quot; : &quot;a key&quot;,
    &quot;value&quot; : &quot;a value&quot;
  }, {
    &quot;key&quot; : &quot;another key&quot;,
    &quot;value&quot; : &quot;another value&quot;,
    &quot;values&quot; : &quot;extra value1&quot; // array is dumped
  } ]
}

Interesting side note

Without the array reference on the input this is a fairly easy problem, it seems the behaviour is not what I am expecting when there is an array reference and the *-beta operations.

simple default

I've gotten rid of the array from the input

simple default input

{
  &quot;element1&quot;: {
    &quot;key&quot;: &quot;a key&quot;,
    &quot;value&quot;: &quot;a value&quot;
  },
  &quot;element2&quot;: {
    &quot;key&quot;: &quot;a key&quot;,
    &quot;value&quot;: &quot;another value&quot;
  }
}

simple default spec

[
  {
    &quot;operation&quot;: &quot;default&quot;,
    &quot;spec&quot;: {
      &quot;element2&quot;: {
        &quot;values&quot;: [&quot;extra value1&quot;, &quot;extra value2&quot;]
      }
    }
  }
]

simple default actual

{
  &quot;element1&quot; : {
    &quot;key&quot; : &quot;a key&quot;,
    &quot;value&quot; : &quot;a value&quot;
  },
  &quot;element2&quot; : {
    &quot;key&quot; : &quot;another key&quot;,
    &quot;value&quot; : &quot;another value&quot;,
    &quot;values&quot; : [ &quot;extra value1&quot;, &quot;extra value2&quot; ]
  }
}

答案1

得分: 0

这似乎是我对规范用法的误解。

看起来你不是插入一个数组,而是要插入每个要插入的元素。

上述规范将两个值插入到值属性中,具体为元素"0"和元素"1"

这也适用于modify-overwrite-beta操作。

这两个规范的结果如下:

{
  "array": [{
    "key": "a key",
    "value": "a value"
  }, {
    "key": "another key",
    "value": "another value",
    "values": ["extra value1", "extra value2"]
  }]
}
英文:

This appears to be my misunderstanding of the usage of the spec.

It appears that you don't insert an array, but each element that you want to insert.

[
  {
    &quot;operation&quot;: &quot;modify-default-beta&quot;,
    &quot;spec&quot;: {
      &quot;array&quot;: {
        &quot;[1]&quot;: {
          &quot;values&quot;: {
            &quot;[0]&quot;: &quot;extra value1&quot;,
            &quot;[1]&quot;: &quot;extra value2&quot;
          }
        }
      }
    }
  }
]

The spec above is inserting both values into the values attribute and specifically into element &quot;[0]&quot; and element &quot;[1]&quot;.

This also works with the modify-overwrite-beta operation

[
  {
    &quot;operation&quot;: &quot;modify-overwrite-beta&quot;,
    &quot;spec&quot;: {
      &quot;array&quot;: {
        &quot;[1]&quot;: {
          &quot;values&quot;: {
            &quot;[0]&quot;: &quot;extra value1&quot;,
            &quot;[1]&quot;: &quot;extra value2&quot;
          }
        }
      }
    }
  }
]

the result of both of these specs is as follows:

{
  &quot;array&quot; : [ {
    &quot;key&quot; : &quot;a key&quot;,
    &quot;value&quot; : &quot;a value&quot;
  }, {
    &quot;key&quot; : &quot;another key&quot;,
    &quot;value&quot; : &quot;another value&quot;,
    &quot;values&quot; : [ &quot;extra value1&quot;, &quot;extra value2&quot; ]
  } ]
}

huangapple
  • 本文由 发表于 2020年8月12日 20:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63376418.html
匿名

发表评论

匿名网友

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

确定