Преобразование многомерного массива в одномерный массив с помощью Dataweave - PullRequest
1 голос
/ 29 июня 2019

Необходимо преобразовать многомерный массив (json) в одномерный массив, повторив родительский атрибут с дочерним атрибутом. Условием является то, что родитель может иметь ребенка или не иметь ребенка. Существует 100 атрибутов, которые необходимо отобразить, поэтому было бы здорово, если бы я мог отображать каждый атрибут без определения имен отдельных атрибутов (если это возможно).

Было бы здорово, если бы эту проблему можно было решить только с помощью .dwl Исходная полезная нагрузка:

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

Ожидаемая полезная нагрузка после перевода - Сценарий 1 - Все атрибуты

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

Ожидаемая полезная нагрузка после перевода - Сценарий 2 - Только некоторые атрибуты

[
  {
    "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",
  }
]

Пытался использовать функции сокращения, групп, но не смог объединить их.

N / A

N / A

1 Ответ

2 голосов
/ 30 июня 2019

Ключ заключается в том, чтобы использовать плющеную карту и вложенную карту.Таким образом, вы можете получить доступ к обоим уровням, чтобы вы могли работать с ними.

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

Для dw 1 это решение

%dw 1.0

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

                parent.child map ((child, index) -> (parent - "child") ++ child) when  (parent.child?)
            otherwise
              [parent]
        )
     )
...