1

Need to convert multi-dimensional array (json) into single dimensional array by repeating parent attribute with child attribute. Condition is that parent may have child or may not have child. There is 100s of attribute that needs to map so would be great if I can map every attribute without defining individual attribute names (if feasible).

It would be great if this problem could be solved by only using .dwl Original payload:

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "child": [
      {
        "childAttribute1": "inner1-1-1",
        "childAttribute2": "inner1-1-2"
      },
      {
        "childAttribute1": "inner1-2-1",
        "childAttribute2": "inner1-2-2"
      },
      {
        "childAttribute1": "inner1-3-1",
        "childAttribute2": "inner1-3-2"
      }
    ]
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "parentAttribute2": "parent2-2",
    "parentAttribute3": "parent2-3",
    "child": [
      {
        "childAttribute1": "inner2-1-1",
        "childAttribute2": "inner2-1-2"
      }
    ]
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
    "parentAttribute2": "parent3-2",
    "parentAttribute3": "parent3-3"
  }
]

Expected payload after translation - Scenario 1 - All attributes

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-1-1",
    "childAttribute2": "inner1-1-2"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-2-1",
    "childAttribute2": "inner1-2-2"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-3-1",
    "childAttribute2": "inner1-3-2"
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "parentAttribute2": "parent2-2",
    "parentAttribute3": "parent2-3",
    "childAttribute1": "inner2-1-1",
    "childAttribute2": "inner2-1-2"
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
    "parentAttribute2": "parent3-2",
    "parentAttribute3": "parent3-3"
  }
]

Expected payload after translation - Scenario 2 - Some attributes only

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-1-1"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-2-1"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-3-1"
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "childAttribute1": "inner2-1-1"
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
  }
]

Tried to use reduce, group functions but was unable to merge them.

n/a

n/a

2
  • Hi @kimmy looks like you paste the same json on the input and on the output. Could you please paste the expected output. Commented Jun 29, 2019 at 12:19
  • Thank you @machaval. I have corrected the output. Commented Jun 30, 2019 at 2:05

1 Answer 1

2

The key is to use the flatten plus a nested map. This way you can access both levels so you can operate with them.

%dw 2.0
output application/json  
---
flatten(payload map 
    ((parent, index) -> 
        if (parent.child?)
            parent.child map ((child, index) -> (parent - "child") ++ child)
        else
          [parent]
    )
 )

For dw 1 this is the solution

%dw 1.0

    %output application/json  
    ---
    flatten (payload map 
        ((parent, index) -> 

                parent.child map ((child, index) -> (parent - "child") ++ child) when  (parent.child?)
            otherwise
              [parent]
        )
     )
Sign up to request clarification or add additional context in comments.

4 Comments

Ok will translate this
thank you for this, I tried copy and pasting your code (including ")" at the end but cannot get this code working. Getting multiple errors. Also I am using %dw 1.0 to do the transformation.
@kimmy I've updated the solution with the one for dw 1
thank you for your help. All good. Catered both scenarios using exactly same input as your dw but instead of "parent", used "parent.parentAttribute1" to get the specific attribute. Thanks again.

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.