richard
richard

Reputation: 47

Transformation of Json array in Dataweave

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

Upvotes: 0

Views: 285

Answers (3)

StackOverflowed
StackOverflowed

Reputation: 759

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

Upvotes: 1

Harshank Bansal
Harshank Bansal

Reputation: 3315

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.
        }
      ]
    })
  })
}

Upvotes: 2

Jason Chiu
Jason Chiu

Reputation: 1

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

Upvotes: 0

Related Questions