1
votes

I need to extract key and values from json input object to form different json output.

I went through the documentation and other questions asked similar to this where I could found out that $$ gives the key but, in my case it is giving me index but not the key name.

The input json looks like this :{ "key2": "val2", "key3": "val3", "key4": "val4", "key5": "val5", "key6": "val6" }

The dataweave code I have written is :

{
"someOtherKey": "val",
properties: {
    entry: payload map

     {  
        key:$$,
        value:$
     }


}

}

After transformation I am getting :

{
 "someOtherKey": "val",
"properties": {
    "entry": [
        {
            "key": 0,
            "value": "val2"
        },
        {
            "key": 1,
            "value": "val3"
        },
        {
            "key": 2,
            "value": "val4"
        },
        {
            "key": 3,
            "value": "val5"
        },
        {
            "key": 4,
            "value": "val6"
        }
    ]
}

}

Here I am expecting output with key name as value for Key

Expected output :

{
"someOtherKey": "val",
"properties": {
    "entry": [{
            "key": "key2",
            "value": "val2"
        },
        {
            "key": "key3",
            "value": "val3"
        },
        {
            "key": "key4",
            "value": "val4"
        },
        {
            "key": "key5",
            "value": "val5"
        },
        {
            "key": "key6",
            "value": "val6"
        }

    ]
}

}

2

2 Answers

1
votes

The tag pluck worked for me. Here is the example :

{
"someOtherKey": "val",
properties: {
    entry: payload pluck

     {  
        key:$$,
        value:$
     }


}

}

0
votes

Use mapObject instead of map

%dw 1.0
%output application/json
---
{
    key: "val",
    key1: "val1",
    properties: {
        entry: payload mapObject {  
            key:$$,
            value:$
         }
    }
}

Hope this helps.