1

I have an input payload (json array) that needs to be enriched with a key-value in a specific index. My requirement was to put the additional key-value (the same for all objects) at index 1, so i've managed to do like this:

Input payload:

[
  {
    "key1": "value1",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  }
]

Script:


%dw 2.0
output application/json
---
payload map (
        ($)[&0] ++ {"key2": "value2"} ++ ($ - "key1")
    )

Output:

[
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  }
]

My question is: how to achieve this dynamically?

Thanks, Marco

  • 1
    i dont know why is this requirement bcause in an object it doesnt impact, as indexing relates to an array not to an object – Anurag Sharma Mar 01 '22 at 11:56
  • Indeed, objects should be accessed by key, not by index, though some implementations allow it – aled Mar 01 '22 at 13:16
  • I have one more idea where we can just add the place (or index) like if we want at second place then just pass 2 with key value, and it will add key value at 2nd place , i have written a function. should i edit my last answer or should i add another answer? – Anurag Sharma Mar 01 '22 at 13:44
  • edited my last answer with one more option addition – Anurag Sharma Mar 01 '22 at 13:55

3 Answers3

2

Please try the below Script. We declared a variable and added it in each object of your input array. you can declare this vaiable in set variable too basedon requirement if this key value you are getting from payload or from any resource.

%dw 2.0
output application/json
var keyValue = {
    "key2": "value2"
} 
---
payload map ((item, index) -> (item) ++ keyValue ) map ((item1, index) ->(item1) mapObject ((value, key, index) ->((key):value ) ) orderBy ((value, key) ->value ))

Another option where you just need to give a place of index(.g, 2 or 3 or 4 as per equirement) to add key value

%dw 2.0
output application/json
fun addKeyAtPosition(in : Object, position : Number,keyValue : Object)=(
    sizeOf(in) match {
        case size if(position <=0 ) -> (keyValue ++ in)
        case size if(position > size) -> (in ++ keyValue)
        case size if(position <= size) -> (in mapObject ((value, key, index) -> 
 (if ((index+1) == position)
 keyValue ++ ((key): value)
 else ((key): value))
))
else -> keyValue ++ in
    }
)

var keyValue= {
    "key2": "value2"
} 
---
payload map ((item, index) -> 
addKeyAtPosition(item,2,keyValue)        // here we are passing the index as 2
)
Anurag Sharma
  • 780
  • 7
  • 31
0
  1. Preserving the keys of payload first so that we can interate from 2nd element to n-1
  2. k[1 to -1] since you want to insert at 1st index position ignoring the 1st key element

DW

%dw 2.0
output application/json
var k=keysOf(payload[0])
---
payload map(item,index)-> (
       [(item[&0])] ++ [{"key2": "value2"}] ++ (k[1 to -1] map ($): (item[$]))   
) reduce($$++$)
        

Alternatitevely You can use MinusMinus to remove an element

%dw 2.0
output application/json
---
payload map (
        ($)[&0] ++ {"key2": "value2"} ++ ($ -- (($)[&0]))
    )

Output

[
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5"
  }
]
Karthik
  • 2,181
  • 4
  • 10
  • 28
  • Thank you for your answer, actually my question is how to insert a key-value in another index, lets say at index 2. How can i select the portion of the object [0 to 1], add the key-value and add the rest of the object [2 to -1]? – Marco Tanda Mar 01 '22 at 10:40
  • @MarcoTanda I have modified the answer. Please check if this helps – Karthik Mar 01 '22 at 12:45
0

First I would remove the key in case it is already present, then add the new key pair dynamically. You could omit the removal if you are sure the key doesn't previously exists in the input elements.

%dw 2.0
output application/json
var key="key2"
var value="value2"
---
payload map (
        $ - key  ++ {(key): value}
)
aled
  • 21,330
  • 3
  • 27
  • 34