0

How to write Dataweave transformation in Anytime Studio for given input and output of Json array. Input:

{
    "result": [{
        "Labels": [{
            "value": [{
                    "fieldName": "firstName",
                    "value": "John"
                },
                {
                    "fieldName": "lastName",
                    "value": "Doe"
                },
                {
                    "fieldName": "fullName",
                    "value": "John Doe"
                }
            ]
        }]
    }]
}

Output:

 {
    "result": [{
        "Labels": [{
            "value": [{
                "firstName": "John",
                "lastName": "Doe",
                "fullName": "John Doe"
            }]
        }]
    }]
 }

https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-reduce Reduce function might be the one should be used Thank you in advance

3
  • Your input and output json are invalid. What is that you are trying to achieve? Commented Jul 13, 2022 at 6:33
  • See corrected json Commented Jul 13, 2022 at 6:36
  • 1
    Should reword the question, the product is "Anypoint Studio". Commented Jul 19, 2022 at 1:21

3 Answers 3

2

You can just use map to map all the arrays to required format. For the value part you can map the values as fieldName: value array and deconstruct them to an object by wrapping the array around parentheses

%dw 2.0
output application/json  
---
{
  result: payload.result map ((item) -> {
    Labels: item.Labels map ((label) -> {
      value: [
        {
          (label.value map ((field) -> 
            (field.fieldName): field.value
          )) //wrap the array, i.e. lavel.value map ... in parentheses so that it will give you individual key pair.
        }
      ]
    })
  })
}
Sign up to request clarification or add additional context in comments.

3 Comments

"Cannot coerce Array ([]) to Key 16| (field.fieldName): field.value ^^^^^^^^^^^^^^^^^^ got this error
Can you confirm the input that you shared in the question? I rechecked the dataweave here developer.mulesoft.com/learn/dataweave and it is working for me with the input that you have shared
Yes, it was my fault, this works! Thank youI
1

You can try below if you are aware that the keyNames will not change:

%dw 2.0
output application/json  
---
payload update {
  case res at .result -> res map (res, resIndex) -> (res update {
      case lbl at .Labels -> lbl map (lbl, lblIndex) -> (lbl update {
          case val at .value -> [
            (val reduce ((item, acc = {}) -> acc ++ {
                (item.fieldName): (item.value)
              }))
          ]
        }
        )
    }
    )
}

Comments

0

Here's 2 caveats and a solution. Your input and output files, both are not valid JSON.

  1. Input file, in your "result" object, "Labels" need curly braces {} since they are objects. Key-value pairs should look like this {key:value} not like that key:value
  2. Output file, inside your "value" arrays, key-value pairs need to have the curlies {key:value}

So here's a valid JSON version of your input

{
 "result": [
  {"Labels": [
        {
          "value": [
            {"fieldName": "firstName","value": "John"},
            {"fieldName": "lastName","value": "Doe"},
            {"fieldName": "fullName","value": "John Doe"}
          ]
        }
      ]},
   {"Labels": [
        {
          "value": [
            {"fieldName": "firstName","value": "John"}
          ]
        }
      ]}
]}  

Here's a solution

%dw 2.0
import keySet from dw::core::Objects

// this is "result"
var layer1key = keySet(payload)[0]
// this is "Labels" and grabs the first Labels, so assumes Labels doesn't change
var layer2 = payload[layer1key]
var layer2key = keySet(layer2[0])[0]
// this is "value"
var layer3 = layer2[layer2key]
var layer3key = keySet(layer3[0][0])[0]
// this is "fieldName" and "value"
var layer4 = layer3 map (x) -> x['value']

var data1 = ((layer1key) : layer4 map (x) -> {
    (layer2key): x map (y) -> {
        (layer3key): y map (z) -> {
            (z['fieldName']):z['value']
        }
    }
})
output application/json
---
data1

And a valid JSON version of your output

{
  "result": [
    {
      "Labels": [
        {
          "value": [
            {
              "firstName": "John"
            },
            {
              "lastName": "Doe"
            },
            {
              "fullName": "John Doe"
            }
          ]
        }
      ]
    },
    {
      "Labels": [
        {
          "value": [
            {
              "firstName": "John"
            }
          ]
        }
      ]
    }
  ]
}

Comments

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.