3

I have a JSON object as below

{
 "a" : "ABCDEFGHIJKL",
 "b" : "B"
}

here each field contains a string (all are of different sizes), I need to divide each field value (i.e string) by 4 and then make it a separate JSON object array and assigned it to a new field So my expected o/p here is as below

[{
        "label": "a",
        "value": [{
                "Line": "ABCD"
            },
            {
                "Line": "EFGH"
            },
            {
                "Line": "IJKL"
            }
        ]
    },
    {
        "label": "b",
        "value": [{
            "Line": "B"
        }]
    }
]

I am using mule4, I tried to use some function of Dataweave but still no luck

Anurag Sharma
  • 780
  • 7
  • 31

3 Answers3

3

Give this a try!! (Updated the input and output which i forgot to put here earlier. Quite early in the AM for me at that point :) )

Input

{
 "a": "YHTUJLOKIYTRUYT", 
 "b": "ERF",
 "c": "ABCDEFGHIJKLMONP",
 "d": "AVBGTRETYU",
 "e": "ABGTIOU",
 "f": "",
 "g": "E"
}

Script

%dw 2.0
output application/json
var divideBy=4
fun returnStringParts(val :String, iteration :Number, size :Number) = {
    result:  if((sizeOf(val) - (iteration*size)) > 0 )val  [((iteration -1)*size) to (iteration*size)-1]  else val [(iteration-1)*size to -1]
}
---
payload mapObject  (value,key,index) -> {
    "label": (key),
    "value" : if (sizeOf(value) > divideBy)  ((1 to (ceil((sizeOf(value)/divideBy )))) map {
        "Line": returnStringParts(value,$,divideBy).result
    }) 
    else
    {
       temp: [{
            "Line": value
        }]
    }.temp
}

Output

{
  "label": "a",
  "value": [
    {
      "Line": "YHTU"
    },
    {
      "Line": "JLOK"
    },
    {
      "Line": "IYTR"
    },
    {
      "Line": "UYT"
    }
  ],
  "label": "b",
  "value": [
    {
      "Line": "ERF"
    }
  ],
  "label": "c",
  "value": [
    {
      "Line": "ABCD"
    },
    {
      "Line": "EFGH"
    },
    {
      "Line": "IJKL"
    },
    {
      "Line": "MONP"
    }
  ],
  "label": "d",
  "value": [
    {
      "Line": "AVBG"
    },
    {
      "Line": "TRET"
    },
    {
      "Line": "YU"
    }
  ],
  "label": "e",
  "value": [
    {
      "Line": "ABGT"
    },
    {
      "Line": "IOU"
    }
  ],
  "label": "f",
  "value": [
    {
      "Line": ""
    }
  ],
  "label": "g",
  "value": [
    {
      "Line": "E"
    }
  ]
}
Salim Khan
  • 4,233
  • 11
  • 15
0

Here's a function that will break a string in equal parts along with a bunch of tests that I used:

%dw 2.0
output application/dw

var tests = [
    "ABCDEFGHIJKLMNOPSQ",
    "A",
    "ABC",
    "",
    "ABCDEFGH",
    "ABCD"
]
fun eqPartsOf(s: String, p: Number) = s match {
    case "" -> [""]
    else -> do {
        var parts = if ((sizeOf(s) mod p) == 0) (sizeOf(s) / p - 1) else (sizeOf(s) / p)
        ---
        0 to parts map (
            (s[($ * p) to ($ * p + p - 1)]) default (s[($ * p) to -1])
        )       
    }
}

---
tests map (
    $ eqPartsOf 4
)

You can can now use it when forming the result you want.

George
  • 2,758
  • 12
  • 16
0

This approach might be similar to George where you need to break down the string to equal parts based on the length you wanted and the size of the string. Using pluck to result to an array and map to iterate through the resulting number of parts.

%dw 2.0
output application/json

var payload = {
 "a" : "ABCDEFGHIJKL",
 "b" : "B"
}

var length = 4

---
payload pluck (value, key) -> 
    using (totalLength = sizeOf(value),
           arrayMaxCount =  if (totalLength >= length) ceil((totalLength / length) - 1)
                            else 0
    )
    {
        "label" : key,
        "value" : (0 to arrayMaxCount) map (innerValue) -> 
        using ( innerMinIndex = innerValue * length,
                innerMaxIndex  = if (totalLength - (innerMinIndex + length - 1) >= 0) innerMinIndex + length - 1
                                 else totalLength - 1
        )
        {
            "Line" : value[innerMinIndex to innerMaxIndex]
        }
    }

This will result to:

[
  {
    "label": "a",
    "value": [
      {
        "Line": "ABCD"
      },
      {
        "Line": "EFGH"
      },
      {
        "Line": "IJKL"
      }
    ]
  },
  {
    "label": "b",
    "value": [
      {
        "Line": "B"
      }
    ]
  }
]
oim
  • 1,141
  • 10
  • 14