Home > Net >  Map an additional key-value in a specific index of a json array using Dataweave 2.0
Map an additional key-value in a specific index of a json array using Dataweave 2.0

Time:03-02

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

CodePudding user response:

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
)

CodePudding user response:

  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"
  }
]

CodePudding user response:

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}
)
  • Related