1

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 Answers 2

2

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

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

     {  
        key:$$,
        value:$
     }


}

}

Sign up to request clarification or add additional context in comments.

Comments

0

Use mapObject instead of map

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

Hope this helps.

1 Comment

Thanks but, this gives me only one :{ "someOtherKey": "val", "properties": { "entry": { "key": "key6", "value": "val6" } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.